首页 > 编程语言 > 详细

(三).数组和循环

时间:2020-05-08 23:12:09      阅读:60      评论:0      收藏:0      [点我收藏+]

数组和循环

遍历数组:foreach(type objName in collection/Array)

string test = "Hello,world!";
foreach (char i in test)
{
    Console.WriteLine(i);
}

借助foreach,只能一一取得数组中的元素,并不能利用这种语句改变数组所存储的元素。
生成随机数: random

const int N = 50;	//const声明常量,不能再次赋值
long[] a = new long[N];	//数组

var r = new Random();
for (int i = 0; i < N; i++)
{
	a[i] = r.Next(0 ,10);	//一般包括左边的下限,不包括右边的上限
}

foreach (long x in a)
{
	Console.WriteLine(x);
}

break会跳出最近的一层循环.

c#预处理指令

region 和 #endregion用来折叠代码

#region 提示文字  
...
...代码区域
...
#endregion  

List

List<int> nums = new List<int>();	//对于集合类,习惯用s结尾

nums.Add(3);
nums.Add(1);
nums.Add(2);

foreach (int num in nums)
{
	Console.WriteLine(num);
}

Console.ReadLine();

(三).数组和循环

原文:https://www.cnblogs.com/jiutianzhiyu/p/12853212.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!