C#bool数据类型和try,catch异常捕获机制

bool数据类型只有两个值,true & false

1 static void Main(string[] args)
2         {
3             int age = 12;
4             int year = 13;
5             bool isRight = age > year;   //定义一个bool类型的变量isRight,将age和year比较的结果赋予isRight
6             Console.WriteLine(isRight);  //bool类型的变量只有两个结果,True&False
7             Console.ReadKey();

 

try{} catch{}异常捕获机制

 1 namespace Cshrap中异常捕获机制
 2 {
 3     class Program
 4     {
 5         static void Main(string[] args)
 6         {
 7             try
 8             {
 9                 Console.WriteLine("请输入您的语文成绩:");
10                 int chinese = Convert.ToInt32(Console.ReadLine());  //为书写方便,可以直接定义变量将用户输入字符串转换
11 
12                 Console.WriteLine("请输入您的数学成绩:");
13                 int math = Convert.ToInt32(Console.ReadLine());
14 
15                 int s = chinese + math;
16                 double avg = (double)s / 2;
17 
18                 Console.WriteLine("您的语文成绩:{0}\n您的数学成绩:{1}\n您的总成绩: {2}\n您的平均成绩:{3}", chinese, math,s,avg);  //编写过程中犯得错误,{}没有加0或1,一定不能忘
19                
20             }
21             catch
22             {
23                 Console.WriteLine("对不起您的输入有误,请重新运行输入");
24             }
25             Console.ReadKey();
26         }
27     }
28 }
29 /*异常捕获机制
30 由try
31   {
32    }
33 和catch
34    {
35     }
36 组成,将有可能出错的代码放到try内,如果代码执行无误,则不执行catch,直接运行Conlose.ReadKey();
37 Conlose.ReadKey();要放在catch{}后
38 */

 

posted @ 2012-12-11 23:58  Tianen  阅读(383)  评论(0编辑  收藏  举报