06day c#判断语句

c#判断语句

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 
  7 namespace _06day判断语句
  8 {
  9     class Program
 10     {
 11 //  判断语句
 12 //C# 提供了以下类型的判断语句。
 13         static void Main(string[] args)
 14         {
 15             /* 局部变量定义 */
 16             int a = 100;
 17             int b = 200;
 18 
 19             // ? : 如果c>a则返回 输入大于100,否则返回 输入小于100
 20             int c=Convert.ToInt32(Console.ReadLine());
 21             Console.WriteLine(c > a ? "输入大于100" : "输入小于100"); 
 22             Console.ReadLine();
 23 
 24             #region if (条件)
 25             /* 使用 if 语句检查布尔条件 */
 26             if (a < 20)
 27             {
 28                 /* 如果条件为真,则输出下面的语句 */
 29                 Console.WriteLine("a 小于 20");
 30             }
 31             Console.WriteLine("a 的值是 {0}", a);
 32             Console.ReadLine();
 33             #endregion
 34 
 35             #region if(条件)else
 36             /* 检查布尔条件 */
 37             if (a < 20)
 38             {
 39                 /* 如果条件为真,则输出下面的语句 */
 40                 Console.WriteLine("a 小于 20");
 41             }
 42             else
 43             {
 44                 /* 如果条件为假,则输出下面的语句 */
 45                 Console.WriteLine("a 大于 20");
 46             }
 47             Console.WriteLine("a 的值是 {0}", a);
 48             Console.ReadLine();
 49             #endregion
 50 
 51             #region if()else if()else
 52             /* 检查布尔条件 */
 53             if (a == 10)
 54             {
 55                 /* 如果 if 条件为真,则输出下面的语句 */
 56                 Console.WriteLine("a 的值是 10");
 57             }
 58             else if (a == 20)
 59             {
 60                 /* 如果 else if 条件为真,则输出下面的语句 */
 61                 Console.WriteLine("a 的值是 20");
 62             }
 63             else if (a == 30)
 64             {
 65                 /* 如果 else if 条件为真,则输出下面的语句 */
 66                 Console.WriteLine("a 的值是 30");
 67             }
 68             else
 69             {
 70                 /* 如果上面条件都不为真,则输出下面的语句 */
 71                 Console.WriteLine("没有匹配的值");
 72             }
 73             Console.WriteLine("a 的准确值是 {0}", a);
 74             Console.ReadLine();
 75             #endregion
 76 
 77             #region 嵌套 if 语句
 78             /* 检查布尔条件 */
 79             if (a == 100)
 80             {
 81                 /* 如果条件为真,则检查下面的条件 */
 82                 if (b == 200)
 83                 {
 84                     /* 如果条件为真,则输出下面的语句 */
 85                     Console.WriteLine("a 的值是 100,且 b 的值是 200");
 86                 }
 87             }
 88             Console.WriteLine("a 的准确值是 {0}", a);
 89             Console.WriteLine("b 的准确值是 {0}", b);
 90             Console.ReadLine();
 91             #endregion
 92 
 93             //一个 switch 语句允许测试一个变量等于多个值时的情况。每个值称为一个 case,且被测试的变量会对每个 switch case 进行检查。
 94             #region switch case
 95             /* 局部变量定义 */
 96             char grade = 'B';
 97 
 98             switch (grade)
 99             {
100                 case 'A':
101                     Console.WriteLine("很棒!");
102                     break;
103                 case 'B':
104                 case 'C':
105                     Console.WriteLine("做得好");
106                     break;
107                 case 'D':
108                     Console.WriteLine("您通过了");
109                     break;
110                 case 'F':
111                     Console.WriteLine("最好再试一下");
112                     break;
113                 default:
114                     Console.WriteLine("无效的成绩");
115                     break;
116             }
117             Console.WriteLine("您的成绩是 {0}", grade);
118             Console.ReadLine();
119             #endregion
120 
121             #region 嵌套switch语句
122             switch (a)
123             {
124                 case 100:
125                     Console.WriteLine("这是外部 switch 的一部分");
126                     switch (b)
127                     {
128                         case 200:
129                             Console.WriteLine("这是内部 switch 的一部分");
130                             break;
131                     }
132                     break;
133             }
134             Console.WriteLine("a 的准确值是 {0}", a);
135             Console.WriteLine("b 的准确值是 {0}", b);
136             Console.ReadLine();
137             #endregion
138 
139 
140         }
141     }
142 }

猜你喜欢

转载自www.cnblogs.com/cuojue/p/10549845.html