C#的语法----程序结构(5)

下面我们做几个while的练习,加深对while循环的理解。

练习1:老师问学生,这道题你会做了么?如果学生答“YES”,则放学。若为“NO”则老师再讲一遍,直到学生会了为止,

       若超过10遍,无论会了与否都要放学。

分析:循环体---老师不停提问,学生不停回答

      循环条件---学生不会,讲的次数小于10.

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 草稿
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             string answer = string.Empty;
14             int i = 0;
15 
16             while (answer != "YES" && i<10)
17             {
18                 Console.WriteLine("这是我第{0}次给你讲了,你会了么?YES/NO",i+1);
19                 answer = Console.ReadLine();
20 
21                 if (answer=="YES")
22                 {
23                     Console.WriteLine("学会了那就放学!!!");
24                     break;
25                 }
26                 i++;
27             }
28             Console.ReadKey();
29         }
30     }
31 }

练习2:提示用户输入用户名和密码,用户名admin密码888888,只要用户输错就重新输入,最多输入三次

分析:循环体---提示用户输入用户名密码 接受 判断

      循环条件---用户名和密码错误 且少于3次

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 草稿
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             int i = 1;
14             string userName = string.Empty;
15             string userPwd = string.Empty;
16 
17             while ((userName != "admin" || userName != "888888") && i <= 3)
18             {
19                 Console.WriteLine("请输入用户名");
20                 userName = Console.ReadLine();
21                 Console.WriteLine("请输入密码");
22                 userPwd = Console.ReadLine();
23                 i++;
24             }
25             Console.ReadKey();
26         }
27     }
28 }

练习3:用户A输入用户名,要求A的用户名不能为空,只要为空,就要求A重新输入

       用户B输入用户名,要求B的用户名不能和A相同,且不能为空,只要跟A的用户名重复,就提示B一直输入。

分析:循环体A---提示A输入用户名,接受不能为空

      循环条件A---用户名为空

      循环体B---提示B输入用户名,接受不能为空

      循环条件B---用户名为空,或者跟A相同

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 草稿
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             string userNameA = string.Empty;
14             
15             while (userNameA == string.Empty)
16             {
17                 Console.WriteLine("请输入用户名,不能为空");
18                 userNameA = Console.ReadLine();
19             }
20             Console.WriteLine("请输入用户名,不能跟A相同,并且不能为空");
21             string userNameB = Console.ReadLine();
22             while (userNameB == string.Empty || userNameB == userNameA)
23             {
24                 if (userNameB==string.Empty)
25                 {
26                     Console.WriteLine("用户名不能为空,请重新输入");
27                     userNameB = Console.ReadLine();
28                 }
29                 else
30                 {
31                     Console.WriteLine("用户名不能和A相同,请重新输入");
32                     userNameB = Console.ReadLine();
33                 }
34             }
35             Console.ReadKey();
36         }
37     }
38 }

练习4:要求用户输入一个数字,然后打印这个数字的2倍,当用户输入q时程序退出(注意异常的处理)

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 草稿
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13 
14             string input = string.Empty;
15             while (input != "q")
16             {
17                 Console.WriteLine("请输入一个数字,我们将打印这个数字的2倍");
18                 input = Console.ReadLine();
19                 if (input!="q")
20                 {
21                     try
22                     {
23                         int number = Convert.ToInt32(input);
24                         Console.WriteLine("您输入的数字的2倍是{0}", number * 2);
25                     }
26                     catch (Exception)
27                     {
28 
29                         Console.WriteLine("输入的字符串不能转换为数字,请重新输入");
30                     }
31                     
32                 }
33                 else
34                 {
35                     Console.WriteLine("输入的是q,程序推出");
36                 }
37             }
38             Console.ReadKey();
39         }
40     }
41 }

练习5:不断要求用户输入一个数字,当用户输入end显示输入的最大值(假设输入的都是整数)

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 草稿
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13 
14             string input = string.Empty;
15             int max = 0;
16 
17             while (input != "end")
18             {
19                 Console.WriteLine("请输入一个数字,输入end我们将显示你的最大值");
20                 input = Console.ReadLine();
21                 if (input != "end")
22                 {
23                     try
24                     {
25                         int number = Convert.ToInt32(input);
26                         if (number > max)
27                         {
28                             max = number;
29                         }
30                     }
31                     catch (Exception)
32                     {
33 
34                         Console.WriteLine("输入非法字符");
35                     }
36                     
37                 }
38                 else
39                 {
40                     Console.WriteLine("您刚才输入的最大值是{0}",max);
41                 }
42             }
43             Console.ReadKey();
44         }
45     }
46 }

这个就是while循环,我们应该对他有了一定的了解:

我对while有一个结论:我们在面对事件型循环时,而且跳出循环的节点为事件时,可以用到while循环。

下面我们再来学习一个while循环的变体。我们用一个例子引出:

小兰明天就要演出了,老师要把明天的演出的歌曲在唱一遍,满意,放学,不满意再练习一遍,直到满意为止(Y/N)

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 草稿
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             Console.WriteLine("老师我唱的你满意么?");
14             string answer = Console.ReadLine();
15             while (answer == "N")
16             {
17                 Console.WriteLine("老师,我们再唱一遍,你满意了么?");
18                 answer = Console.ReadLine();
19             }
20             Console.ReadKey();
21         }
22     }
23 }

我们可以看到在while循环的外面 我们有重复性的接受了一遍,所以先做了一遍,然后在区循环判断,这样的循环我们可以用do-while循环

do-while循环

语法:

do

{

    循环体;

}while(循环条件);

执行过程:程序首先会执行do中的循环体,执行完成后,判断while中的循环条件,如果成立,则继续执行do中的循环体,如果不成立,则跳出

          do-while循环。

特点:先循环,在判断,最少执行一遍循环体。

所以上例可以变形为:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 草稿
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             string answer = string.Empty;
14             do
15             {
16                 Console.WriteLine("老师,我唱的你满意么?yes/no");
17                 answer = Console.ReadLine();
18             } while (answer=="no");
19             Console.WriteLine("放学回家~~~~");
20             Console.ReadKey();
21         }
22     }
23 }
 

猜你喜欢

转载自www.cnblogs.com/LiyuLi/p/12091957.html