Video

传智播客-C#基础-第四天

03 判断闰年

04 while循环

05 while循环练习

06 while循环练习

07 while循环的3个练习

08 break的用法

 

Note

1.while循环

1 while(循环条件)
2             {
3                 循环体;
4             }
while

执行过程:

当满足循环条件,才进行循环

循环条件一般为为bool类型的值或者关系表达式或者逻辑表达式

循环条件为ture,执行循环体

当循环体执行完成一道后,继续回到循环条件进行判断

如果成立,则执行,否则跳出while循环

每个循环都有这个一行代码,能够改变循环条件,使之终有一天不再成立

如果没有,则是死循环while(true)

注:在写while循环的时候,一定要分析循环体和循环条件

2.break

1)break:跳出switch-case语句

2)break:跳出循环

单独放在循环中没意义,一般和if配合使用,表示满足某个条件,跳出循环

 

Practise

请用户输年份,输出该月的天数(结合之前如何判断闰年)

 1 using System;
 2 using static System.Console;
 3 
 4 namespace ConsoleApp3
 5 {
 6     class Program
 7     {
 8         static void Main(string[] args)
 9         {
10             WriteLine("请输入一个年份");
11             int year = Convert.ToInt32(ReadLine());
12             WriteLine("请输入一个月份");
13             int month = Convert.ToInt32(ReadLine());
14             int day = 0;
15 
16             switch (month)
17             {
18                 case 1:
19                 case 3:
20                 case 5:
21                 case 7:
22                 case 8:
23                 case 10:
24                 case 12: day = 31;
25                     break;
26                 case 2: if((year % 400 == 0) ||(year %4 ==0 && year % 100 != 0))
27                     {
28                         
29                        day = 29;
30                     }
31                         
32                     else
33                     {
34                         day = 28;
35                     }
36                         break;
37                 default: day = 30;
38                     break;
39             }
40             WriteLine($"{year}年{month}月{day}天");
41             ReadKey();
42         }
43     }
44 }
天数

求1到100之间所有整数的和

 1 using System;
 2 using static System.Console;
 3 
 4 namespace ConsoleApp5
 5 {
 6     class Program
 7     {
 8         static void Main(string[] args)
 9         {
10             int i = 1;
11             int sum = 0;
12 
13             while(i <= 100)
14             {
15                 sum += i;
16                 i++;
17             }
18             WriteLine($"{sum}");
19             ReadKey();
20         }
21     }
22 }
5050

提示用户输入yes或者no,只要不是yes或者no,就一直提示用户重新输入,只能输入yes或者no

 1 using System;
 2 using static System.Console;
 3 
 4 namespace ConsoleApp6
 5 {
 6     class Program
 7     {
 8         static void Main(string[] args)
 9         {
10             WriteLine("请输入yes或者no");
11             string str = ReadLine();
12 
13             while (str != "yes" && str != "no")
14             {
15                 WriteLine("请输入yes或者no");
16                 str = ReadLine();
17             }
18 
19             WriteLine("搞对了!!!");
20             ReadKey();
21         }
22     }
23 }
Yes

输入班级人数,然后依次输入学员成绩,计算班级学员的平均成绩和总成绩

 1 using System;
 2 using static System.Console;
 3 using static System.Convert;
 4 
 5 namespace ConsoleApp7
 6 {
 7     class Program
 8     {
 9         static void Main(string[] args)
10         {
11             WriteLine("请输入班级人数");
12             int count = Convert.ToInt32(ReadLine());
13             
14             int i = 0;
15             int sum = 0;
16 
17             while(i < count)
18             {
19                 WriteLine ($"请输入第{i+1}位学生的成绩") ;
20                 try 
21                 {
22                 int score = Convert.ToInt32(ReadLine());
23                 sum += score;
24                 i++;
25                 }
26                 catch
27                 {
28                     WriteLine("输入有误,请重新输入");
29                 }
30             }
31             WriteLine($"总成绩:{sum},平均成绩:{sum/count}");
32             ReadKey();
33         }
34     }
35 }
总成绩

老师问学生,这道题你会做了吗?如果学生答“会了”则可以放学

如果学生不会做,则老师再讲一遍,再问学生是否会做了

 1 using System;
 2 using static System.Console;
 3 using static System.Convert;
 4 
 5 namespace ConsoleApp8
 6 {
 7     class Program
 8     {
 9         static void Main(string[] args)
10         {
11             WriteLine("这道题目你会了吗?");
12             string answer = ReadLine();
13             int i = 0;
14 
15             while (answer != "yes" && i < 10)
16             {
17                 WriteLine("只是我第{i+1}讲给你听,这道题目你会了吗?");
18                 answer = ReadLine();
19                 i++;
20             }
21 
22             WriteLine("放学啦!!!");
23             ReadKey();
24         }
25     }
26 }
放学

2006年培养学员80000人,每年增长25%

请问按此增长速度,到哪一年培养学员的人数达到20万人

 1 using System;
 2 using static System.Console;
 3 using static System.Convert;
 4 
 5 namespace ConsoleApp9
 6 {
 7     class Program
 8     {
 9         static void Main(string[] args)
10         {
11             int year = 2006;
12             double people = 80000;
13 
14             while( people <= 200000)
15             {
16                 people *= 1.25;
17                 year++;
18             }
19 
20             WriteLine($"到{year}年的时候,人数达到20万");
21             ReadKey();
22               
23         }
24     }
25 }
20万