c# for的循环疑问public static int[] shuzu = new int[20] { 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20 }; //shuzu[19] = shuzu[18];//shuzu[18] = shuzu[17];//shuzu[17] = shuzu[16];//shuzu[16] = shuzu[15];//shuzu[15] = shuzu[14];//sh
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/16 10:54:07
c# for的循环疑问public static int[] shuzu = new int[20] { 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20 }; //shuzu[19] = shuzu[18];//shuzu[18] = shuzu[17];//shuzu[17] = shuzu[16];//shuzu[16] = shuzu[15];//shuzu[15] = shuzu[14];//sh
c# for的循环疑问
public static int[] shuzu = new int[20] { 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20 };
//shuzu[19] = shuzu[18];
//shuzu[18] = shuzu[17];
//shuzu[17] = shuzu[16];
//shuzu[16] = shuzu[15];
//shuzu[15] = shuzu[14];
//shuzu[14] = shuzu[13];
//shuzu[13] = shuzu[12];
//shuzu[12] = shuzu[11];
//shuzu[11] = shuzu[10];
//shuzu[10] = shuzu[9];
//shuzu[9] = shuzu[8];
//shuzu[8] = shuzu[7];
//shuzu[7] = shuzu[6];
//shuzu[6] = shuzu[5];
//shuzu[5] = shuzu[4];
//shuzu[4] = shuzu[3];
//shuzu[3] = shuzu[2];
//shuzu[2] = shuzu[1];
//shuzu[1] = shuzu[0];
//shuzu[0]=50;
for (j = 19; j < 1; j=j-1)
{
shuzu[j] = shuzu[j - 1];
}
shuzu[0]=50;
两个功能一样不?
我要将他放入一个循环里,用这个使shuzu[]中的数据依次变为50,为什么第一种比较麻烦的方法可以依次改变数据.(数组中的元素依次变位50)
而第二种方法就shuzu[0]=50,其余的不变,循环许多次都不变.
c# for的循环疑问public static int[] shuzu = new int[20] { 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20 }; //shuzu[19] = shuzu[18];//shuzu[18] = shuzu[17];//shuzu[17] = shuzu[16];//shuzu[16] = shuzu[15];//shuzu[15] = shuzu[14];//sh
两个功能差不多,不过你的for循环有问题:
1. 条件应为:j>=0;
2.shuzi[0]为最小值不会自动减1,如果要自减1就应为:shuzu[19]=50,再用减循环就Ok了
我写两种你参考一下:
1
shuzu[19]=50;
for(int j=19;j>=0;j--)
{
shuzu[j]=shuzu[j-1];
}
2
shuzu[0]=50;
for(int j=1;j