有人可以告诉我这段代码有什么问题吗?我的木板不应该装满吗?(我上面有两个函数,一个函数生成一个随机数,另一个函数打印数组)
功能:
void map(char a[3][3]){
int row, column;
for(row=0;row<3;row++){
for(column=0;column<3;column++){
printf ("%c ", a[row][column]);
}
printf("\n \n \n \n \n");
}
}
float getRand() {
return ((rand() / (RAND_MAX+1.0))*9)+1;
}
主要的:
srand( time(NULL) );
int r,c,ui,cntr,cntr2,ran,g,x;
char kb, aray[3][3]={{'.','.','.'},{'.','.','.'},{'.','.','.'}};
getRand();
ran=getRand();
for(cntr2=0;cntr2<5;cntr2++){
g=ran/3;
x=ran%3;
if(aray[g][x]=='.'){
aray[g][x]='O';
system("cls");
map(aray);
}
else{
cntr2--;
}
}
假定getRand
返回0到9之间的数字,则循环一旦循环一次,就会有一个O
in array[g][x]
。
在下一次迭代中,保持不变ran
,所以g
和x
的值与以前相同。现在,array[g][x]
包含一个O
and,因此else语句将执行,并且undoescntr2++
仍将保持value 1
,从而导致无限循环。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句