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

最后这一个循环是我们未来最最常用的for循环,所以篇幅较长,敬请谅解。

我不知道,大家在用while循环的时候,再写控制循环次数的时候,是不是总将i++忘记写,所以while还是有时候不太好用的,

那么,在我们已知循环次数的情况下,我们可以使用for循环来避免。

for循环

语法:

for(表达式1;表达式2;表达式3)

{

    循环体;

}

表达式1:声明循环变量,记录循环次数。

表达式2:循环条件。

表达式3:改变循环条件的代码,使之终会不再成立。

我们写一下下面这样一个练习:找出100~999之间的水仙花数(百位3+十位3+个位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 
14             for (int i = 100; i <=999; i++)
15             {
16                 int bai = i / 100;
17                 int shi = i % 100 / 10;
18                 int ge = i % 10;
19 
20                 if (bai*bai*bai+shi*shi*shi+ge*ge*ge == i)
21                 {
22                     Console.WriteLine("水仙花数有{0}",i);
23                 }
24                 Console.ReadKey();
25             }
26         }
27     }
28 }

练习2:乘法口诀(矩形输出)

 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             for (int i = 1; i <= 9; i++)
14             {
15                 for (int j = 1; j <= 9; j++)
16                 {
17                     Console.Write("{0}*{1}={2}\t",i,j,i*j);
18                 }
19                 Console.WriteLine();
20             }
21             Console.ReadKey();
22         }
23     }
24 }

 练习3:循环录入5人信息并计算平均年龄,如果录入数据出现负数或大于100,立即停止输入并报错

 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             bool b = true;
15             for (int i = 0; i < 5; i++)
16             {
17                 Console.WriteLine("请输入第{0}个人的成绩",i+1);
18                 try
19                 {
20                     int age = Convert.ToInt32(Console.ReadLine());
21                     if (age >= 0 && age <= 100)
22                     {
23                         sum += age;
24 
25                     }
26                     else
27                     {
28                         Console.WriteLine("输入的年龄有误");
29                         b = false;
30                         break;
31                     }
32                 }
33                 catch 
34                 {
35 
36                     Console.WriteLine("输入的年龄不正确,程序退出!!!");
37                     b = false;
38                     break;
39                 }
40               
41             }
42             if (b)
43             {
44                 Console.WriteLine("5个人的平均年龄是{0}", sum / 5);
45             }
46             Console.ReadKey();
47         }
48     }
49 }

在前几节中,我们介绍了控制循环程序流向的关键字break,下面我们再介绍一个。

continue:立即结束本次循环,判断循环条件,如果成立,则进入下一次循环,否则退出循环。

练习1:用while continue实现实现计算1~100之间的除了能被7整除之外的所有整数的和

 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             int i = 1;
15             while (i < 100)
16             {
17                 if (i % 7 == 0)
18                 {
19                     i++;
20                     continue;
21                 }
22                 sum += i;
23                 i++;
24             }
25             Console.WriteLine(sum);
26             Console.ReadKey();
27         }
28     }
29 }

练习2:找出100内的所有质数(只能被1和本身整除的数字)

 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             for (int i = 2; i <= 100; i++)
14             {
15                 bool b = true;
16                 for (int j = 2; j < i; j++)
17                 {
18                     if (i % j == 0)  //除尽了 不是质数
19                     {
20                         b = false;
21                         break;
22                     }
23                 }
24                 if (b)
25                 {
26                     Console.WriteLine(i);
27                 }
28 
29             }
30             Console.ReadKey();
31         }
32     }
33 }

以上1~6节是我们对程序的流程控制的全部介绍,下面我们做一个小引子,引出我们以后一个难点和重点,方法和面向对象。

下面先介绍一个函数:随机数(Random())

步骤如下:

(1)创建能够产生随机数的对象。Random r=new Random();

(2)让产生随机数的的这个对象调用方法产生随机数。int rNumber = r.Next(a,b);


猜你喜欢

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