小练习——关于 语句

1.输入年份,月份,天,判断这个日期是否正确?

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

2.输入年份,月份显示这个月有多少天?

 1  static void Main(string[] args)
 2         {
 3             Console.Write("请输入年份:");
 4             int year = Convert.ToInt32(Console.ReadLine());
 5             Console.Write("请输入月份:");
 6             int month = Convert.ToInt32(Console.ReadLine());
 7 
 8             if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
 9             {
10                 Console.WriteLine("本月是31天");
11             }
12             else if (month == 4 || month == 6 || month == 9 || month == 11)
13             {
14                 Console.WriteLine("本月是30天");
15             }
16             else if (month == 2)
17             {
18                 if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
19                 {
20                     Console.WriteLine("29天");
21                 }
22                 else
23                 {
24                     Console.WriteLine("28天");
25                 }
26             }
27             else
28             {
29                 Console.WriteLine("月份有问题");
30             }
31         }

  

3.axx+bx+c==0(a!=0)。输入a,b,c给这个一元二次方程,显示根的个数?

 1 static void Main(string[] asdfasdf)
 2         {
 3             Console.WriteLine("一元二次方程a*x*x+b*x+c == 0,请输入a,b,c的值");
 4             int a = Convert.ToInt32(Console.ReadLine());
 5             int b = Convert.ToInt32(Console.ReadLine());
 6             int c = Convert.ToInt32(Console.ReadLine());
 7 
 8             if (a == 0)
 9             {
10                 Console.WriteLine("不是一元二次方程");
11             }
12             else
13             {
14                 int delta = b * b - 4 * a * c;
15                 if (delta > 0)
16                 {
17                     Console.WriteLine("两个不等实根");
18                 }
19                 else if (delta == 0)
20                 {
21                     Console.WriteLine("两个相等实根");
22                 }
23                 else
24                 {
25                     Console.WriteLine("无实根");
26                 }
27             }
28         }

4.男士身高与体重的关系是:身高-100=体重; 女士:身高-110=体重。(自己试着做)
上下浮动3公斤属正常。
输入性别,身高,体重,输出:正常?偏胖?偏瘦?

 1  static void Main(string[] args)
 2         {
 3             string xingbie;
 4             int shengao, tizhong;
 5 
 6             //输入:
 7             Console.Write("性别:");
 8             xingbie = Console.ReadLine();
 9             Console.Write("身高:");
10             shengao = Convert.ToInt32(Console.ReadLine());
11             Console.Write("体重:");
12             tizhong = Convert.ToInt32(Console.ReadLine());
13 
14             //运算
15             if (xingbie == "")
16             {
17                 int nbz = shengao - 100; //标准体重
18                 if (nbz - tizhong >= -3 && nbz - tizhong <= 3)
19                 {
20                     Console.WriteLine("正常,请继续保持");
21                 }
22                 else if (nbz - tizhong < -3)
23                 {
24                     Console.WriteLine("偏胖,请意饮食");
25                 }
26                 else
27                 {
28                     Console.WriteLine("偏瘦,注意营养");
29                 }
30             }
31             else if (xingbie == "")
32             {
33                 int vbz = shengao - 110; //标准体重
34                 if (vbz - tizhong >= -3 && vbz - tizhong <= 3)
35                 {
36                     Console.WriteLine("正常,请继续保持");
37                 }
38                 else if (vbz - tizhong < -3)
39                 {
40                     Console.WriteLine("偏胖,请意饮食");
41                 }
42                 else
43                 {
44                     Console.WriteLine("偏瘦,注意营养");
45                 }
46             }
47             else
48             {
49                 Console.WriteLine("只能识别人类");
50             }
51 
52         }

5.做一个跟计算机猜拳的小游戏。0-剪刀,1-石头,2-布
要求输出0,1,2,计算机生成随机数,与人类输入的相比较判断谁胜了。

计算机生成随机数:
Random rand = new Random();
int c = rand.Next(3);

6.做一个算缘分的小游戏:
输入男方姓名,女方姓名,输出缘分指数,给出建议。

 1  static void Main(string[] args)
 2         {
 3             Console.Write("男方姓名:");
 4             string nan = Console.ReadLine();
 5             Console.Write("女方姓名:");
 6             string nv = Console.ReadLine();
 7 
 8             Random rand = new Random();
 9             int n = rand.Next(100);
10             n++;
11 
12             string jianYi = "";
13             if (n > 0 && n < 30)
14             {
15                 jianYi = "分手吧";
16             }
17             else if (n >= 30 && n < 60)
18             {
19                 jianYi = "一起努力";
20             }
21             else if (n >= 60 && n <= 80)
22             {
23                 jianYi = "幸福一对";
24             }
25             else 
26             {
27                 jianYi = "鸳鸯配";
28             }
29 
30             Console.WriteLine("{0}和{1}的缘分指数是:{2}。建议:{3}", nan, nv, n,jianYi);
31         }

显示的结果:

原文地址:https://www.cnblogs.com/yuyu1993/p/5392847.html