C#整理3——运算符和语句

运算符:

一、算术运算符:
+ - * / % ——取余运算

取余运算的应用场景:
1.奇偶数的区分。

2.把数变化到某个范围之内。——彩票生成。

3.判断能否整除。——闰年、平年。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Class2
 9     {
10         //输入一个年份判断是闰年,还是平年。
11         static void ccc(string[] args)
12         {
13             int year;
14             //输入
15             Console.Write("请输入一个年份:");
16             year = Convert.ToInt32(Console.ReadLine());
17 
18             //运算
19             //能被400整除;或能被4整除,但不能被100整除。
20             if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
21             {
22                 Console.WriteLine("是闰年");
23             }
24             else
25             {
26                 Console.WriteLine("是平年");
27             }
28         }
29     }
30 }
View Code

 

 

++(自增运算) --(自减运算)——它只能对变量进行运算。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             int a = 5;
13             a++;
14             //7++; //错误。
15             Console.WriteLine(a);//a = 6;
16         }
17     }
18 }
View Code

 

1.前自增/前自减
先进行自增/自减运算,然后再进行其它运算。可以简单认为前自增/前自减的优先级是最高。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             int a = 5, b;
13             b = ++a;
14             Console.WriteLine("a=" + a + ";b=" + b); //结果应当a=6,b=6
15         }
16     }
17 }
View Code

 

2.后自增/后自减
先进行其它运算,当其它运算都完成后,再进行自增/自减运算。可以简单认为是后自增/后自减优先级是最低的。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             int a = 5, b;
13             b = a++;
14             Console.WriteLine("a=" + a + ";b=" + b);//结果应当是a=6,b=5
15         }
16     }
17 }
View Code

 

二、关系运算符:——用来判断式子成立与否
== != > >= < <=
注意:
双等号不要写成单等号


三、逻辑运算符:&&,||都双操作数,!单操作数
&& 与(并且)

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             int a = 5, b = 6;
13             Console.WriteLine(a > b && a > 0); //false;
14             //true???
15         }
16     }
17 }
View Code

 


|| 或(或者)

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             int a = 5, b = 6;
13             Console.WriteLine((a > b) || (a > 0)); //true
14             //false??
15         }
16     }
17 }
View Code

 


! 非 ——取反

优先级:
一般来说:
1.算术运算术的优先级要高关系运算符;关系运算符的优先级要高于逻辑运算符
2.逻辑非优先级最高。逻辑与要高于逻辑或。
3.如果在不确定,就加小括号。

四、其它运算符:
1.赋值运算符:=。把右边的结果送到左边去。左边只能是变量。
2.复合运算符:+= -= *= /= %= 知道就行。
a+=5; <==> a = a + 5
3.条件运算符:三目运算符 ?: 。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             int a=5,b=6,c;
13             c = a > b ? a : b;
14             Console.WriteLine( c )
15 
16 
17         }
18     }
19 }
View Code

 

作业:
1.游泳池

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             const double PI = 3.14;
13             const int BAR_UNIT_PRICE = 25;
14             const int BRICK_UNIT_PRICE = 85;
15             //输入
16             int a, b;
17             Console.Write("请输入泳池半径:");
18             string s1 = Console.ReadLine();
19             a = Convert.ToInt32(s1);
20             Console.Write("请输入广场半径:");
21             string s2 = Console.ReadLine();
22             b = Convert.ToInt32(s2);
23 
24             //运算
25             double l = 2 * PI * a;    //求泳池的周长
26             double area1 = PI * a * a;//泳池的面积。
27             double area2 = PI * b * b;//总面积。
28             double area = area2 - area1;//广场面积
29             double barPrice = l * BAR_UNIT_PRICE; //护栏的总造价
30             double brickPrice = area * BRICK_UNIT_PRICE;//广场的造价。
31 
32             //输出
33             Console.WriteLine("护栏的长度是" + l + "米,广场砖的总面积是" + area + "平米,总造价为" + (barPrice + brickPrice) + "");
34  
35 
36         }
37     }
38 }
View Code

2.老狼老狼几点了

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             Console.Write("老狼老狼几点了?");
13             string hour = Console.ReadLine();
14             int a = Convert.ToInt32(hour ),b;
15             string c;
16    
17             b = a > 12 ? a - 12 : a;
18             c = a > 12 ? "下午" : "上午";
19             Console.WriteLine("现在是"+c+b+"点了");
20  
21 
22 
23             
24      
25  
26 
27 
28             
29 
30 
31         }
32     }
33 }
View Code

3.输入三个数a,b,c。输出最大的。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Class1
 9     {
10         static void Main(string[] args)
11         {
12             int a, b, c,d,max;
13             Console.Write("请输入三个数:");
14             string a1 = Console.ReadLine();
15             string b1 = Console.ReadLine();
16             string c1 = Console.ReadLine();
17 
18             a = Convert.ToInt32(a1);
19             b = Convert.ToInt32(b1);
20             c = Convert.ToInt32(c1);
21             d = a > b ? a : b;
22             max = d > c ? d : c;
23             Console.WriteLine("三个数中最大的数是"+max);
24 
25 
26 
27 
28 
29         }
30     }
31 }
View Code

 

二、语句:
顺序,分支,循环。

(一)顺序:略
分支:判断--表达式。if(){}
四大类:
1.if
if (age > 18)
{
Console.WriteLine("可以去当兵!");
}

注意:if表达式后面只管一句话,可以省略掉{};如果if表达式后面需要管多句话,则必须加{}

2.if...else...
if (age > 18)
{
Console.WriteLine("成年了!");
Console.WriteLine("可以去当兵!");
}
else
{
Console.WriteLine("还没长大!");
Console.WriteLine("回家上学去!");
}
注意:
1.else后面不要加分号。
2.else后面不要加小括号。


3.if...else if...else if...else 多分支。

4.if嵌套。
if(...)
{
if(...)
{
}
else
{
}
}
else
{
if(...)
{
}
else
{
}
}
分层、分类来解决问题的思路。
练习:
1.老狼几点了。凌晨,上午,下午,晚上。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Class1
 9     {
10         static void Main(string[] args)
11         {
12             //输入
13             Console.Write("老狼老狼几点了?");
14             string s = Console.ReadLine();
15             int hour = Convert.ToInt32(s);
16 
17             if (hour >= 0 && hour < 6) // 0<hour<6:错误
18             {
19                 Console.WriteLine("凌晨" + hour + "点了");
20             }
21             else if (hour >= 6 && hour <= 12)
22             {
23                 Console.WriteLine("上午" + hour + "点了");
24             }
25             else if (hour > 12 && hour < 18)
26             {
27                 hour -= 12;
28                 Console.WriteLine("下午" + hour + "点了");
29             }
30             else if (hour >= 18 && hour < 24)
31             {
32                 hour -= 12;
33                 Console.WriteLine("晚上" + hour + "点了");
34             }
35             else
36             {
37                 Console.WriteLine("不可识别的时间!");
38             }
39 
40 
41 
42 
43 
44         }
45     }
46 }
View Code

2.判断一元二次方向根的情况。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Class1
 9     {
10         //判断一元二次方程根的情况。
11         static void Main(string[] args)
12         {
13             int a, b, c;
14            //输入;
15             Console.Write("请输入系数a:");
16             a = Convert.ToInt32(Console.ReadLine());
17             Console.Write("请输入系数b:");
18             b = Convert.ToInt32(Console.ReadLine());
19             Console.Write("请输入系数c:");
20             c = Convert.ToInt32(Console.ReadLine());
21 
22             //运算输出
23             if (a == 0)
24             {
25                 Console.WriteLine("不是一元二次方程");
26             }
27             else
28             {
29                 int d = b * b - 4 * a * c;
30                 if (d > 0)
31                 {
32                     Console.WriteLine("两个不等实根");
33                 }
34                 else if (d == 0)
35                 {
36                     Console.WriteLine("两个相等实根");
37                 }
38                 else
39                 {
40                     Console.WriteLine("无实根");
41                 }
42             }
43         }
44     }
45 }
View Code

3.输入一个年份,判断是闰年还是平年。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Class2
 9     {
10         //输入一个年份判断是闰年,还是平年。
11         static void Main(string[] args)
12         {
13             int year;
14             //输入
15             Console.Write("请输入一个年份:");
16             year = Convert.ToInt32(Console.ReadLine());
17 
18             //运算
19             //能被400整除;或能被4整除,但不能被100整除。
20             if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
21             {
22                 Console.WriteLine("是闰年");
23             }
24             else
25             {
26                 Console.WriteLine("是平年");
27             }
28         }
29     }
30 }
View Code

4.称体重。

男人的标准体重是:体重(kg)=身高(cm)-100。
女人的标准体重是:体重(kg)=身高(cm)-110。
上下浮动3公斤属正常
要求输入性别、身高和体重,输出正常,偏胖,偏瘦

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Class3
 9     {
10         static void Main(string[] args)
11         {
12             string sex;
13             int weight, height;
14             //输入
15             Console.Write("性别(男,女):");
16             sex = Console.ReadLine();
17             Console.Write("身高(CM):");
18             height = Convert.ToInt32(Console.ReadLine());
19             Console.Write("体重(KG):");
20             weight = Convert.ToInt32(Console.ReadLine());
21 
22             int biaoZhunTiZhong=0;
23             //运算输出
24             if(sex == "")
25             {
26                 //求标准体重
27                 biaoZhunTiZhong = height - 100;
28             }
29             else if(sex == "")
30             {
31                 //求标准体重
32                 biaoZhunTiZhong = height - 110;
33             }
34             else
35             {
36                 Console.WriteLine("性别不正确");
37             }
38 
39             //求体重差
40             int tiZhongCha = weight - biaoZhunTiZhong;
41             if (tiZhongCha >= -3 && tiZhongCha <= 3)
42             {
43                 Console.WriteLine("体重正常,继续保持!");
44             }
45             else if (tiZhongCha < -3)
46             {
47                 Console.WriteLine("偏瘦,注意增加营养");
48             }
49             else
50             {
51                 Console.WriteLine("偏胖,注意运动减肥");
52             }
53         }
54     }
55 }
View Code

5.输入年、月、日,判断是否是个正确的日期。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Class4
 9     {
10         static void Main(string[] args)
11         {
12             int year = 0, month = 0, day = 0;
13             //输入
14             Console.Write("请输入年:");
15             year = Convert.ToInt32(Console.ReadLine());
16             Console.Write("请输入月:");
17             month = Convert.ToInt32(Console.ReadLine());
18             Console.Write("请输入日:");
19             day = Convert.ToInt32(Console.ReadLine());
20 
21             //判断运算输出
22             //判断年份是否正确
23             if(year < 0 || year>9999)
24             {
25                 Console.WriteLine("年输入不正确");
26             }
27             else
28             {
29                 Console.WriteLine("年正确");
30             }
31 
32             //判断月份是否正确
33             if (month < 1 || month > 12)
34             {
35                 Console.WriteLine("月输入不正确");
36             }
37             else
38             {
39                 Console.WriteLine("月正确");
40             }
41 
42             //判断天是否正确
43             if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
44             {
45                 if(day < 1 || day>31)
46                 {
47                     Console.WriteLine("天输入错误,大月份最多是31天");
48                 }
49                 else
50                 {
51                     Console.WriteLine("天正确");
52                 }
53             }
54             else if (month == 4 || month == 6 || month == 9 || month == 11)
55             {
56                 if (day < 1 || day > 30)
57                 {
58                     Console.WriteLine("天输入错误,小月份最多是30天");
59                 }
60                 else
61                 {
62                     Console.WriteLine("天正确");
63                 }
64             }
65             else if(month == 2)
66             {
67                 //闰年与平年的判断
68                 if(year%400==0 || year%4==0&&year%100!=0)
69                 {
70                     //闰年
71                     if (day < 1 || day > 29)
72                     {
73                         Console.WriteLine("天输入错误,闰年2月份最多是29天");
74                     }
75                     else
76                     {
77                         Console.WriteLine("天正确");
78                     }
79                 }
80                 else
81                 {
82                     //平年
83                     if (day < 1 || day > 28)
84                     {
85                         Console.WriteLine("天输入错误,平年2月份最多是28天");
86                     }
87                     else
88                     {
89                         Console.WriteLine("天正确");
90                     }
91                 }
92             }
93         }
94     }
95 }
View Code

 

posted on 2015-03-03 15:44  酸甜sky  阅读(220)  评论(0编辑  收藏  举报