class Program
{
static void Main(string[] args)
{
//练习题{ 33, 36, 66, 23 }{36,66,33,23}
int[] arrA = { 33, 36, 66, 23 };
int[] arrB = new int[arrA.Length];
ShowClass.Show1(arrA, "前: ");
arrA.CopyTo(arrB, 0);
Array.Copy(arrA, 1, arrB, 0, 2);
Array.Copy(arrA, 0, arrB, 2, 1);
ShowClass.Show1(arrB, "后: ");
//练2:{ 33, 36, 66, 23 } {33,66,23,36}
int[] arrC = new int[arrA.Length];
ShowClass.Show1(arrA, "前: ");
arrA.CopyTo(arrC, 0);
Array.Copy(arrA, 1, arrC, arrA.Length - 1, 1);
Array.Copy(arrA, 2, arrC, 1, 1);
Array.Copy(arrA, arrA.Length - 1, arrC, 2, 1);
ShowClass.Show1(arrC, "后: ");
//练 {36,66,33,23} {33,66,23,36}
int[] arrD = new int[arrB.Length];
ShowClass.Show1(arrB, "前: ");
arrB.CopyTo(arrD, 0);
Array.Copy(arrB, 0, arrD, 3, 1);//5个参数 参数1:被复制数组,参数2:被复制参数下标;参数3:复制数组;参数4:复制数组下标;参数5:被复制数组的元素长度个数;
Array.Copy(arrB, 2, arrD, 0, 1);
Array.Copy(arrB, 3, arrD, 2, 1);
ShowClass.Show1(arrD, "后: ");
// 随机数
Random myRandom = new Random();
Console.WriteLine(myRandom.Next(1, 100));//表示1-100的随机数,包含1,不包含100;
// 练 随机10个数 范围1-100;奇数放奇数位;偶数放偶数位;
int[] arr1 = new int[10];
int number = 0;
int ou = 0;
int ji = 1;
Random myRan = new Random();
//for循环
for (int i = 0; i < arr1.Length; i++)
{
chongfu1:
number = myRan.Next(20, 80);
if (number % 2 == 0 && ou < 9)
{
arr1[ou] = number;
ou += 2;
}
else if (number % 2 != 0 && ji < 10)
{
arr1[ji] = number;
ji += 2;
}
if (ji - ou > 1 || ji < ou)
{//随机出来的奇数不能连续两个;奇数位-偶数位等于1可以,大于1就不不行,奇数位小于偶数位时重复;
goto chongfu1;
}
}
int j = 0;//随机1-10赋值到数组里面去;
while (true)
{
number = myRan.Next(1, 100);
j++;
if (j == 10)
{
break;
}
arr1[j] = number;
}
ShowClass.Show1(arr1, "随机数:\0\0\0");
//goto
int[] arrX = new int[10];
Random MyRan = new Random();
int indexO = 0;
int indexJ = 1;
int count = 0;
int num = 0;
chongfu:
num = MyRan.Next(20, 80);
if (num % 2 == 0 && indexO < 10)
{
arrX[indexO] = num;
indexO += 2;
count++;
}
else if (num % 2 == 1 && indexJ < 10)
{
arrX[indexJ] = num;
indexJ += 2;
count++;
}
if (count < 10)
{
goto chongfu;
}
ShowClass.Show1(arrX, "20-80随机数:\0\0\0");
while (true)
{
num = MyRan.Next(1, 10);
if (num % 2 == 0 && indexO < 10)
{
arrX[indexO] = num;
indexO += 2;
count++;
}
else if (num % 2 == 1 && indexJ < 10)
{
arrX[indexJ] = num;
indexJ += 2;
count++;
}
if (count == 10)
{
break;
}
}
ShowClass.Show1(arrX, "while随机数:");
Console.ReadKey();
}
}
unity游戏开发-C#语言基础篇(Array数组(二))
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...