.Net基础篇_学习笔记_第六天_for循环的几个练习

 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             bool b = true;                                                                             //让代码满足某些条件的时候才去执行
14             int sum = 0;
15             for (int i = 1; i <=5; i++)
16             {
17                 Console.WriteLine("请输入录入第{0}个人的年龄为:",i);
18                 try
19                 {
20                     int _age = Convert.ToInt32(Console.ReadLine());
21                     if (_age > 0 && _age < 100)
22                     {
23                         sum += _age;
24                     }
25                     else
26                     {
27                         b = false;
28                         Console.WriteLine("您输入的年龄不在范围内,程序退出");
29                         break;
30                     }
31                 }
32                 catch
33                 {
34                     Console.WriteLine("您输入的年龄格式不正确,程序退出");
35                     b = false;
36                     break;
37                 }
38 
39             }
40             if (b)
41             {
42                 Console.WriteLine("您录入5个人的平均年龄为:{0}岁", sum / 5);
43             }
44            
45             Console.ReadKey();
46         }
47     }
48 }
bool类型的应用,让代码满足某些条件的时候才去执行。

练习二:用户名和密码的登陆——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             Console.WriteLine("请输入您的用户名:");
14             string _name=Console.ReadLine();
15             Console.WriteLine("请输入您的密码");
16             string _psd= Console.ReadLine();
17             while (_name!="admin"||_psd!="8888")
18             {
19                 Console.WriteLine("\n请输入的用户名或密码有误,请重新输入");
20                 Console.WriteLine("请再次输入您的用户名:");
21                 _name = Console.ReadLine();
22                 Console.WriteLine("请再次输入您的密码");
23                 _psd = Console.ReadLine();
24                
25             }
26             Console.WriteLine("您输入的用户名和密码正确,程序可以被使用");
27             Console.ReadKey();
28         }
29     }
30 }

另一种方式

 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             while (true)
14             {
15                 Console.WriteLine("请输入您的用户名:");
16                 string _name = Console.ReadLine();
17                 Console.WriteLine("请输入您的密码");
18                 string _psd = Console.ReadLine();
19                 if (_name == "admin" || _psd == "8888")
20                 {
21                     Console.WriteLine("您输入的用户名和密码正确,程序可以被使用");
22                     break;
23                 }
24                 else
25                 {
26                    
27                     Console.WriteLine("\n请输入的用户名或密码有误,请重新输入");
28                 }
29                 
30             }   
31             Console.ReadKey();
32         }
33     }
34 }

 练习三:

 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 sum = 0;
14             for (int i = 1; i <=100; i++)
15             {
16                 sum += i;
17                 if (sum>20)
18                 {
19                     Console.WriteLine("累加值大于20的当前数是{0}",i);
20                     break;
21                 }
22             }
23                
24             Console.ReadKey();
25         }
26     }
27 }

 

posted @ 2017-07-21 09:24  MR_L先生  阅读(224)  评论(0编辑  收藏  举报