这是主要功能,其中另外9个功能被调用。我将向您展示另一个功能,以便了解我的目标。
int main() {
char B[rows][columns];
char answer;
int newgame;
printf("Are you ready to play? Answer with y (or Y) for yes or n (or N) for no\n");
scanf("\n%c",&answer);
while(answer!='N' && answer!='n'){
fillArray(B);
printArray(B);
answer='y';
while(answer!='N' && answer!='n'){
vasilhs(B);
printArray(B);
check_draw(B);//I will place an if statement after the check_draw function
check_vertical_win(B);
check_horizontal_win(B);
check_right_diagonal_win(B);
check_left_diagonal_win(B);
antipalos_paikths(B);
printArray(B);
check_draw(B);
check_vertical_win(B);
check_horizontal_win(B);
check_right_diagonal_win(B);
check_left_diagonal_win(B);
}
printf("Do you want to play again?\n");
scanf("\n\n%c",&answer);
}
}//end of main function
//function check_draw
void check_draw(char A[rows][columns]){
int i,j;
int newgame;
//if statement will check in an matrix if the following elements are equal to '-'
if(A[0][0]!='-' && A[0][1]!='-' && A[0][2]!='-' && A[0][3]!='-' && A[0][4]!='-' && A[0][5]!='-' && A[0][6]!='-'){
printf("The result is a draw");
newgame=10;
}
}
因此,当函数check_draw中的if语句为True时,变量newgame将获得值10。
if(newgame==10){
break;
}
为什么当我在check_draw调用之后放置此if语句时,它不会中断while循环吗?
最佳答案
从您的代码实现中:
void check_draw(char A[rows][columns]){
int i, j;
int newgame;
//if statement will check in an matrix if the following elements are equal to '-'
if (A[0][0] != '-' && A[0][1] != '-' && A[0][2] != '-' && A[0][3] != '-' && A[0][4] != '-' && A[0][5] != '-' && A[0][6] != '-'){
printf("The result is a draw");
newgame = 10;
}
}
int newgame
这是check_draw
函数的本地副本。如果另一个具有相同名称的变量作为全局变量存在,则不应在
int newgame
函数中声明check_draw
。它将制作本地副本以供使用。建议:
int newgame;// Write it outside main
void check_draw(char A[rows][columns]){
int i, j;
//if statement will check in an matrix if the following elements are equal to '-'
if (A[0][0] != '-' && A[0][1] != '-' && A[0][2] != '-' && A[0][3] != '-' && A[0][4] != '-' && A[0][5] != '-' && A[0][6] != '-'){
printf("The result is a draw");
newgame = 10;
}
}
如果在main之外写
int newgame;
而不在check_draw
函数中进行任何本地复制,则newgame = 10;
函数中的check_draw
语句将更改全局newgame
的值。关于c - 在while循环中爆发,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43541826/