c#3种基本的条件判断语句有

1.if

2.if...else...

3.switch

1.4.1 if结构

c#中if结构的语法与java完全相同,即

1.

if(表达式)

{

代码块

}

2.

if...else...结构如下

if()

{

代码块1

}

else

{

代码块2

}

3.

多重if结构如下

if(表达式1)

{

代码块1

}

else if(表达式2)

{

代码块2

}

else if(表达式3)

{

代码块3

}

......

else

{

代码块

}

4.嵌套if结构就是在if中再嵌入if结构,即

if(表达式1)

{

  if(表达式2)

  {

  代码块1

  }

  else

  {

  代码块2

  }

}

else

{

代码块3

}

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace HellowWorld
 8 {
 9    /// <summary>
10    /// 该程序实现向控制台输出一条消息
11    /// </summary>
12    /// <param name="args"></param>
13    class Program
14    {
15       //程序的入口
16       static void Main(string[] args)
17       {
18          int price = 4000;//机票原价
19          int month;
20          int type;
21          string info1 = "please input the month when you go out:1~12";
22          string info2 = "please choose first-class or economy-class?first-class 1,economy-class 2";
23          string info3 = "your plane ticket price is {0} $";
24          Console.WriteLine(info1);
25          month = int.Parse(Console.ReadLine());
26          Console.WriteLine(info2);
27          type = int.Parse(Console.ReadLine());
28          if (month >= 5 && month <= 10)//旺季
29          {
30             if (type == 1)//头等舱
31             {
32                Console.WriteLine(info3, price * 0.9);
33             }
34             else if (type == 2)//经济舱
35             {
36                Console.WriteLine(info3, price * 0.75);
37             }
38          }
39          else //淡季
40          {
41             if (type == 1)//头等舱
42             {
43                Console.WriteLine(info3, price * 0.6);
44             }
45             else if (type == 2)//经济舱
46             {
47                Console.WriteLine(info3, price * 0.3);
48             }
49          }
50          Console.ReadLine();
51       }
52    }
53 }
View Code

运行结果

注意:

1.只有当满足外层if条件时才会判断内层if的条件

2.else与离它最近的那个缺少else的if相匹配

规范:

1.为使if结构清晰,应把每个if或else包含的语句都用大括号括起来

2.相匹配的一对if和else应该左对齐

3.内层的if结构相对于外层的if结构要有一定的缩进

1.4.2switch结构

java c#

switch(int/char 表达式)

{

  case 常量表达式1:

  语句1;

  break;//可以没有

  case 常量表达式2:

  语句2;

  break;//可以没有

  ......

  default:

  语句n;

}                                  

switch(int/char/string 表达式)

{

  case 常量表达式1:

  语句1;

  break;//必须有

  case 常量表达式2:

  语句2;

  break;//必须有

  ......

  default:

  语句n;

  break;//必须有

}

switch(int/char/string 表达式)

{

  case 常量表达式1:

  case 常量表达式2:

  语句2;

  break;//必须有

  ......

  default:

  语句n;

  break;//必须有

}    

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  例

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace HellowWorld
 8 {
 9    /// <summary>
10    /// 该程序实现向控制台输出一条消息
11    /// </summary>
12    /// <param name="args"></param>
13    class Program
14    {
15       //程序的入口
16       static void Main(string[] args)
17       {
18          string name1 = "Jack";
19          string name2 = "Tom";
20          string subject1 = "c#";
21          string subject2 = "java";
22          int score1 = 91;
23          int score2 = 89;
24          Console.WriteLine("please choose to output which person's information,Jack or Tom?");
25          string choice = Console.ReadLine();
26          Console.WriteLine("{0}t{1}t{2}","name","subject","score");
27          switch (choice)
28          {
29             case "Jack":
30                Console.WriteLine("{0}t{1}t{2}",name1,subject1,score1);
31                break;
32             case "Tom":
33                Console.WriteLine("{0}t{1}t{2}", name2, subject2, score2);
34                break;
35             default:
36                Console.WriteLine("sorry,there is no the person you search for!");
37                break;
38          }
39          Console.ReadLine();
40       }
41    }
42 }
View Code

运行结果如图

经常会有忘记写break造成出错,

1.改正错误时往往从最上面的一条错误信息开始。

2.为了方便定位,需要VS显示行号,在vs中可通过单击菜单 工具>选项>文本编辑器>c#,选中右边的显示下的行号即可。

内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!