数组
数组:是在内存中连续存储同一类型的数据的空间
因为在实际应用中需要存储同一类型的大量数据,所以有了数组
数组的定义:数据类型 [] 数组名 =new 数据类型[长度];
例:int [] a = new int [10];
数据类型 [] 数组名 =new 数据类型[]{值,中间用逗号隔开};
例:int [] b = new int []{1,2,3,4,5};
数组的赋值与取值:使用下标来操作数组中的每个元素
赋值:数组名 [下标] = 值 ;
取值:数组名 [下标] ;
注:数组中元素的索引都是从0开始的,最大的下标值要比数组的长度小1
数组的好处:
1、一次定义多个存储数据的空间
2、可以与for循环配合使用,使用循环变量作为数据的下标
例:随机抽奖
思路:先定义两个数组 a,b,a数组写人名,b数组写奖品,用for循环随机生成一个下标,最后输出
System.Threading.Thread.Sleep(100); //停100毫秒
Console.Clear(); //清屏
Console.Write("请输入你的名子:");
string name = Console.ReadLine();
string[] a = new string[] { "张三", "李四", "王五", "赵六", "田七", "朱八" };
string[] b = new string[] { "吃饭", "睡觉", "玩游戏", "看电影", "追MM", "学习" };
string[] c = new string[] { "可爱的","装B的","傻拉巴几的","萌萌的","高贵的"};
string[] d = new string[] { "在电影院","在墙角","在垃圾筒边","在学校","在河边"};
Random rand = new Random();
int i = rand.Next(a.Length);
int j = rand.Next(b.Length);
int k = rand.Next(c.Length);
int w = rand.Next(d.Length);
Console.WriteLine(c[k]+name+"和"+a[i]+"一起"+d[w]+b[j]);
//string[] a = new string[] { "张三", "李四", "王五", "赵六", "田七", "朱八" };
//string[] b = new string[] { "彩电", "笔记本电脑", "iPhone6", "iPadMini", "谢谢" };
//Random rand = new Random();
//for (int i = 0; i < 50; i++)
//{
// int n = rand.Next(a.Length); //生成随机下标
// Console.WriteLine(a[n]);
// System.Threading.Thread.Sleep(100); //停100毫秒
// Console.Clear();
//}
//int m = rand.Next(a.Length); //生成随机下标
//int k = rand.Next(b.Length);
//Console.WriteLine("恭喜" + a[m] + ",中奖了,中了一个" + b[k]);
//Console.WriteLine("恭喜猪八中奖了,中了一台笔记本电脑");
//string[] s = new string[50];
////输入
//for (int i = 0; i < s.Length; i++)
//{
// Console.Write("请输入你的好朋友的名子:");
// s[i] = Console.ReadLine();
//}
////打印输出
//for (int i = 0; i < s.Length; i++)
//{
// Console.WriteLine(s[i]);
//}
//string[] s = new string[5]; //定义
//s[0] = "张三";//赋值
//s[1] = "李四";
//s[2] = "王五";
//s[3] = "赵六";
//s[4] = "田七";
////s[5] = "朱八";
//int n = 5;
//Console.WriteLine(s[3+n]);