【c#.Net】c#.Net基础入门(数组和循环)

一、数组

定义一个新数组:

   int[] a = new int[20];

也可以

4种循环:

1.while循环

 2.for循环

 3.foreach

x把a中数据全都读一遍,但是有缺陷,没有办法读取下标。

4.do-while

 for循环随机生成50个随机数并用foreach循环打印出来:

 1 static void Main(string[] args)
 2             {
 3 
 4                 const int N = 50;
 5                 long[] a = new long[N];
 6                 var r = new Random();
 7                 for (int i= 0; i!=N; i++)
 8             {
 9                 a[i] = r.Next(1, 10);
10 
11              }
12                 foreach(long x in a)
13                     {
14                         Console.WriteLine(x);
15 
16                     }
17                 Console.Read();
18             }
View Code

 查找数组中的最大值:

 

 排序数字(冒泡排序):

 

让代码分模块region

 列表

 或

 输出最大值:

 continue----结束当前循环,进行下一次循环

break---直接跳出循环

猜你喜欢

转载自www.cnblogs.com/zhuzhubaoya/p/12384586.html
今日推荐