C#有趣小例子(三)

简介: C#有趣小例子(三)

实例1 语文和数学成绩

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入语文成绩");
 
            string strChinese = Console.ReadLine();
 
            int chinese = Convert.ToInt32(strChinese);
 
            Console.WriteLine("请输入数学成绩");
 
            int math = Convert.ToInt32(Console.ReadLine());
 
            bool result1=chinese>90 && math > 90;
            Console.WriteLine(result1);
 
            Console.ReadKey();
 
            bool result2=chinese > 90 || math > 90;
             
            Console.WriteLine(result2);
 
            Console.ReadKey();
 
 
        }
    }
}

判断闰年

快捷键 CW TAB(两下)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApp24
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入年份");
            string strYear = Console.ReadLine();
 
            int year = Convert.ToInt32(strYear);
 
            bool result = (year % 400 == 0) ||( year % 4 == 0 && year % 100 != 0);
 
            Console.WriteLine(result);
 
 
            Console.ReadKey();
 
            
        }
    }
}
True闰年

if结构

判断是否是闰年
 Console.WriteLine("请您输入年份");
 
            int year = Convert.ToInt32(Console.ReadLine());
 
            bool result = (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
 
            if (result)
            {
                Console.WriteLine("闰年");
            }
            //if语句:首先判断括号中的条件,如果条件成立则执行大括号中的语句,如果条件为false,则直接跳过大括号,执行后面或下面的代码。
            //有条件的执行一条语句,有可能一条都不执行
 
            else
            {
                Console.WriteLine("不是闰年");
            }
            Console.ReadKey();

类型转换

如何判断成绩段

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApp32
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("输入成绩");
            int score = Convert.ToInt32(Console.ReadLine());
 
            if (score>=90)
            {
                Console.WriteLine("A");
            }
            else
            {
                if (score>=80)
                {
                    Console.WriteLine("B");
                }
                else
                {
                    if (score>=70)
                    {
                        Console.WriteLine("C");
                    }
                    else
                    {
                        if (score>= 60)
                        {
                            Console.WriteLine("D");
                        }
                        else
                        {
                            Console.WriteLine("不合格");
                        }
                        
 
                    }
                }
            }
            Console.ReadKey();
        }
    }
}

优化版成绩分段

1.  Console.WriteLine("输入成绩");
            int score = Convert.ToInt32(Console.ReadLine());
 
            if (score >= 90)
            {
                Console.WriteLine("A");
            }
            else if (score >= 80)
            {
                Console.WriteLine("B");
            }
            else if (score >= 70)
            {
                Console.WriteLine("C");
            }
            else if (score >= 60)
            {
                Console.WriteLine("D");
            }
            else
            {
                Console.WriteLine("不合格");
            }
            Console.ReadKey();


目录
相关文章
|
2月前
|
C#
C#的小例子和总结(二)
C#的小例子和总结(二)
25 0
|
2月前
|
C#
C#的小例子和总结(四)
C#的小例子和总结(四)
14 1
|
Java Spring 数据采集
需要些例子
教您使用java爬虫gecco抓取JD全部商品信息教您使用DynamicGecco抓取JD全部商品信息 Gecco+Spring+Mybatis完整例子,下载妹子图美女图片 结合spring的插件gecco-spring 结合htmlunit的插件ge...
1027 0
|
数据安全/隐私保护
|
算法 Android开发 计算机视觉
|
SQL 数据库 关系型数据库