C#学习Day2

2021-05-19 22:04发布

1.比较运算符

比较运算符组成的表达式,其结果是bool类型

            //int a, b;
            //a = 10;
            //b = 20;
            //bool res1= a == b;//比较是否相等
            //bool res2 = a != b;//比较是否不相等
            //Console.WriteLine(res1);
            //Console.WriteLine(res2);


2.逻辑运算符

 逻辑与(&&),逻辑或(||),逻辑非(!)

逻辑运算符两边的值均是bool类型

            int a = 10, b = 20, c = 30;
            //逻辑与&&:两边的值都是true的时候,整个表达式的结果为true 只要有一个结果是假,整个表达式的结果就是false 真真为真
            bool res = (a < b) && (a < c);
            Console.WriteLine(res);
            //逻辑或||:两边的值都是false的时候,整个表达式的结果为false,只要有个值为true,整个表达式的结果就位true
            bool res1 = (a < b) || (a > c);
            Console.WriteLine(res1);
            //逻辑非!:取反
            Console.WriteLine(!res1);

短路机制

当逻辑与&&前面为假则不会运行后面的代码直接判断为假

当逻辑或||前面为真则不会运行后面的代码直接判断为真


3.if语句

语法格式: if(bool类型的表达式){语句} bool表达式后面不能加';'

            int a = 10, b = 20;
            if (a < b) //快捷方式:输入if 双击tab
            {
                console.writeline("成立");
            }

            //练习1
            char c = Convert.ToChar(Console.ReadLine());
            //int c=console.read() 直接读输入键的asc值 也会成立 
            if (c == 'm' || c == 'M') //因为系统会自动比较''里的asc值
            {
                Console.WriteLine("man");
            }
            else
            {
                Console.WriteLine("women");
            }

            //练习2
            int year = Convert.ToInt32(Console.ReadLine());
            if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
            {
                Console.WriteLine("是闰年");
            }
            else
            {
                Console.WriteLine("不是闰年");
            }

            //练习3
            int fs = Convert.ToInt32(Console.ReadLine());
            if (fs >= 90)
            {
                Console.WriteLine("优秀");
            }
            else if (fs >= 80)
            {
                Console.WriteLine("优");
            }
            else if (fs >= 70)
            {
                Console.WriteLine("良");
            }
            else if (fs >= 60)
            {
                Console.WriteLine("及格");
            }
            else
            {
                Console.WriteLine("不及格");
            }

            //练习4
            int res = Console.Read();
            //字符之间比较会直接比较asc码值
            if (res >= 65 && res <= 90)
            {
                Console.WriteLine("这是大写字母");
            }
            else if (res >= 97 && res <= 122)
            {
                Console.WriteLine("这是小写字母");
            }
            else if (res >= 48 && res <= 57)
            {
                Console.WriteLine("这是数字");
            }
            else
            {
                Console.WriteLine("other");
            }


4.条件运算符

            int a = Convert.ToInt32(Console.ReadLine());
            int b = Convert.ToInt32(Console.ReadLine());
            int max = 0;
            max = a > b ? a : b;//a大于输出a 否者输出b
            string str = a > b ? "a>b" : "a<b";//也可以返回字符串


5.switch...case语句

 注意事项:

1.如果case下面有代码,则一定要有break;

2.当程序运行break之后,会直接跳出switch后面大括号,执行大括号后面的代码

3.当所有的case的值与switch后面表达式的值不相同时,会执行default下面的代码,如果没有default语句,则直接跳出开关

4.case的位置可以不固定

5.如果case下面没有代码,可以省去break,此时该case就和下面的case共用一段代码


//键盘输入1输出春天,2夏天,3秋天,4冬天
            int res = Convert.ToInt32(Console.ReadLine());
            switch (res)
            {
                case 1:
                    Console.WriteLine("春天");
                    break;
                case 2:
                    Console.WriteLine("夏天");
                    break;
                case 3:
                    Console.WriteLine("秋天");
                    break;
                case 4:
                    Console.WriteLine("冬天");
                    break;
                default:
                    Console.WriteLine("no");
                    break;
            }