HQ-day12-13 C#控制台程序习题集①

1.输入三个整数,xyz,最终以从小到大的方式输出。利用if嵌套。

 1             Console.Write("请输入数字x:");
 2             int x = int.Parse(Console.ReadLine());
 3             Console.Write("请输入数字y:");
 4             int y = int.Parse(Console.ReadLine());
 5             Console.Write("请输入数字z:");
 6             int z = int.Parse(Console.ReadLine());
 7             int zhong;
 8             if (x < y && x < z)
 9             {
10                 if (y < z)
11                 {
12                 }
13                 else
14                 {
15                     zhong = z;
16                     z = y;
17                     y = zhong;
18                 }
19             }
20             else if (y < x && y < z)
21             {
22                 zhong = y;
23                 y = x;
24                 x = zhong;
25                 if (y < z)
26                 {
27                 }
28                 else
29                 {
30                     zhong = z;
31                     z = y;
32                     y = zhong;
33                 }
34             }
35             else
36             {
37                 zhong = z;
38                 z = x;
39                 x = zhong;
40                 if (y < z)
41                 {
42                 }
43                 else
44                 {
45                     zhong = z;
46                     z = y;
47                     y = zhong;
48                 }
49             }
50 
51             Console.WriteLine(x);
52             Console.WriteLine(y);
53             Console.WriteLine(z);

输入三个整数,xyz,最终以从小到大的方式输出。利用中间变量。

 1             Console.Write("请输入一个数x=");
 2             int x = int.Parse(Console.ReadLine());
 3             Console.Write("请输入一个数y=");
 4             int y = int.Parse(Console.ReadLine());
 5             Console.Write("请输入一个数z=");
 6             int z = int.Parse(Console.ReadLine());
 7             int zhong;
 8             if (x > y)
 9             {
10                 zhong = y;
11                 y = x;
12                 x = zhong;
13             }
14             if (x > z)
15             {
16                 zhong = z;
17                 z = x;
18                 x = zhong;
19             }
20             if (y > z)
21             {
22                 zhong = y;
23                 y = z;
24                 z = zhong;
25             }
26             Console.WriteLine(x);
27             Console.WriteLine(y);
28             Console.WriteLine(z);

输入三个整数,xyz,最终以从小到大的方式输出。利用条件运算符。

 1             Console.Write("请输入数字x:");
 2             int x = int.Parse(Console.ReadLine());
 3             Console.Write("请输入数字y:");
 4             int y = int.Parse(Console.ReadLine());
 5             Console.Write("请输入数字z:");
 6             int z = int.Parse(Console.ReadLine());
 7             int a = (x < y) ? x : y;
 8             int b = (a < z) ? a : z; //b最小
 9             int c = (x > y) ? x : y;
10             int d = (c > z) ? c : z;//d最大
11             int e = (a > b) ? a : ((x < y) ? ((y < z) ? y : z) : ((x < z) ? x : z));
12             //                        x最小,比y和z :      y最小 比x和z
13 
14             Console.WriteLine(b);
15             Console.WriteLine(e);
16             Console.WriteLine(d);

“现在几点了?”键盘键入小时数,判断是上午还是下午。打印出来现在是上午几点还是下午几点。利用条件运算符。

1             Console.Write("现在几点了?");
2             double a = double.Parse(Console.ReadLine());
3             string b = (a > 12) ? "现在是pm" + (a - 12) : "现在是am" + a;
4             Console.WriteLine(b);

相亲过程:你有房子么?你有钱么?你有能力么?

【结婚吧】【先买房子在结婚】【先赚钱再买房子再结婚】都没有【拜拜~~】

利用if嵌套做相亲过程。

 1             Console.WriteLine("女:你有房子吗?");
 2             string yes = Console.ReadLine();
 3             if (yes == "")
 4             {
 5                 Console.WriteLine("女:我们结婚吧!");
 6             }
 7             else
 8             {
 9                 Console.Write("女:你有钱吗?");
10                 yes = Console.ReadLine();
11                 if (yes == "")
12                 {
13                     Console.WriteLine("女:先买房子再结婚吧!");
14                 }
15                 else
16                 {
17                     yes = Console.ReadLine();
18                     Console.Write("女:你有能力吗?");
19                     if (yes == "")
20                     {
21                         Console.WriteLine("女:先赚钱再买房子再结婚!");
22                     }
23                     else
24                     {
25                         yes = Console.ReadLine();
26                         Console.WriteLine("女:拜拜!");
27                     }
28                 }
29             }

6.输入年月日,看看格式是否正确。利用if嵌套。

 1             Console.Write("请输入年份");
 2             int y = int.Parse(Console.ReadLine());
 3             if (y >= 0 && y < 9999)
 4             {
 5                 Console.Write("请输入月份");
 6                 int m = int.Parse(Console.ReadLine());
 7                 if (m > 0 && m <= 12)
 8                 {
 9                     Console.Write("请输入日期:");
10                     int d = int.Parse(Console.ReadLine());
11                     if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
12                     {
13                         if (d > 0 && d <= 31)
14                         {
15                             Console.WriteLine("您输入的是" + y + "年," + m + "月," + d + "");
16                         }
17                         else
18                         {
19                             Console.WriteLine("您的输入有误!");
20                         }
21                     }
22                     else if (m == 4 || m == 6 || m == 9 || m == 11)
23                     {
24                         if (d > 0 && d <= 30)
25                         {
26                             Console.WriteLine("您输入的是" + y + "年," + m + "月," + d + "");
27                         }
28                         else
29                         {
30                             Console.WriteLine("您的输入有误!");
31                         }
32                     }
33                     else
34                     {
35                         if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0)
36                         {
37                             if (d > 0 && d <= 29)
38                             {
39                                 Console.WriteLine("您输入的是" + y + "闰年," + m + "月," + d + "");
40                             }
41                             else
42                             {
43                                 Console.WriteLine("您的输入有误!");
44                             }
45                         }
46                         else
47                         {
48                             if (d > 0 && d <= 28)
49                             {
50                                 Console.WriteLine("您输入的是" + y + "平年," + m + "月," + d + "");
51                             }
52                             else
53                             {
54                                 Console.WriteLine("您的输入有误!");
55                             }
56                         }
57                     }
58 
59                 }
60                 else
61                 {
62                     Console.WriteLine("您的输入有误!");
63                 }
64             }
65             else
66             {
67                 Console.WriteLine("您的输入有误!");
68             }

6.做人机猜拳,剪刀石头布。利用switch case。

 1             Console.WriteLine("人机猜拳游戏");
 2             Console.WriteLine("1.剪刀2.包袱3.锤;你出哪个?");
 3             int a = int.Parse(Console.ReadLine());
 4             if (a <= 0 || a > 3)
 5             {
 6                 Console.WriteLine("输入有误");
 7             }
 8             else
 9             {
10                 switch (a)
11                 {
12                     case 1:
13                         Console.WriteLine("你出的是剪子");
14                         break;
15                     case 2:
16                         Console.WriteLine("你出的是包袱");
17                         break;
18                     case 3:
19                         Console.WriteLine("你出的是锤");
20                         break;
21                 }
22 
23                 System.Threading.Thread.Sleep(1000);
24                 Random ran = new Random();
25                 int b = ran.Next(3);
26                 string[] c = new string[3] { "剪子", "包袱", "" };
27                 Console.WriteLine("电脑出的是:" + c[b]);
28                 if (a - b == 3 || a - b == 0)
29                 {
30                     Console.WriteLine("你赢了!");
31                 }
32                 if (a - b == -1 || a - b == 2)
33                 {
34                     Console.WriteLine("你输了!");
35                 }
36                 else
37                 {
38                     Console.WriteLine("平局!");
39                 }
40 
41             }
42             Console.ReadLine();

输入一个正整数,求1!+2!+3!+...+n!。利用for循环嵌套。

 1             Console.Write("请输入一个正整数:");
 2             int a = int.Parse(Console.ReadLine());
 3             int sum = 0;
 4             for (int i = 1; i <= a; i++)
 5             {
 6                 int ji = 1;
 7                 for (int j = 1; j <= i; j++)
 8                 {
 9                     ji *= j;
10                 }
11                 sum += ji;
12             }
13             Console.WriteLine(sum);

找出100以内与7有关的数并打印,并求出他们的和。利用for循环+if。

1             int sum = 0;
2             for (int i = 1; i <= 100; i++)
3             {
4                 if (i % 7 == 0 || i % 10 == 7 || i / 10 == 7)
5                 {
6                     Console.WriteLine(i);
7                     sum += i;
8                 }
9             } Console.WriteLine(sum);
原文地址:https://www.cnblogs.com/Itwonderful/p/5292124.html