好的,我很抱歉,我知道有一千个do-while循环如何工作以及如何退出它们的例子。我发誓,我已经尝试过所有这些。对于我的生活,当用户输入0时,我无法退出。必须有一些我过度看的东西。我是如何让一个人在其他if循环中工作,但主要的是超出我。我很感激任何帮助或指导。
int main()
{
char connectBoard[6][7];
int i , j;
char c;
int turn = 1;
char player1 = 'x';
char player2 = 'o';
int spot;
for( i = 0; i < 6; i++) //sets up 2D array board
{
for( j = 0; j < 7; j++)
{
c = '.';
connectBoard[i][j] = c;
}
}
for( i = 0; i < 6; i++)// prints out 2D array board
{
for( j = 0; j < 7; j++)
{
printf("%c", connectBoard[i][j]);
}
printf("\n");
}
printf("=======\n");
printf("1234567\n");
do{
if( turn%2 != 0 ) //player1 turn
{
for( i = 5; i < 6; i++)
{
for(j = 0; j < 1; j++)
{
printf(" Player %c, drop your piece in which column (1-7 or 0 to quit): ", player1);
scanf("%d", &spot);
j = spot - 1;
if( connectBoard[i][j] == '.' )
{
connectBoard[i][j] = 'x';
}
else if( connectBoard[i][j] == 'x' || connectBoard[i][j] == 'o' )
{
do
{
i--;
}while(connectBoard[i][j] == 'x' || connectBoard[i][j] == 'o');
connectBoard[i][j] = 'x';
if( i == -1 )
{
printf("***Bad entry, try again: ");
scanf("%d", &spot);
j = spot - 1;
i = 5;
connectBoard[i][j] = 'x';
}
}
turn++;
}
break;
}
printf("\n");
for( i = 0; i < 6; i++)// prints out 2D array board
{
for( j = 0; j < 7; j++)
{
printf("%c", connectBoard[i][j]);
}
printf("\n");
}
printf("=======\n");
printf("1234567\n");
}
if( turn%2 == 0 ) //player2 turn
{
for( i = 5; i < 6; i++)
{
for(j = 0; j < 1; j++)
{
printf(" Player %c, drop your piece in which column (1-7 or 0 to quit): ", player2);
scanf("%d", &spot);
j = spot - 1;
if( connectBoard[i][j] == '.' )
{
connectBoard[i][j] = 'o';
}
else if( connectBoard[i][j] == 'x' || connectBoard[i][j] == 'o' )
{
do
{
i--;
}while(connectBoard[i][j] == 'x' || connectBoard[i][j] == 'o');
connectBoard[i][j] = 'o';
if( i == -1)
{
printf("***Bad entry, try again: ");
scanf("%d", &spot);
j = spot - 1;
i = 5;
connectBoard[i][j] = 'o';
}
}
turn++;
}
break;
}
printf("\n");
for( i = 0; i < 6; i++)// prints out 2D array board
{
for( j = 0; j < 7; j++)
{
printf("%c", connectBoard[i][j]);
}
printf("\n");
}
printf("=======\n");
printf("1234567\n");
}
}while( spot != 0 );
return 0;
}
答案 0 :(得分:1)
读取点变量后,您可以检查值是否为0并且中断:
printf(" Player %c, drop your piece in which column (1-7 or 0 to quit): ", player2);
scanf("%d", &spot);
if (spot == 0)
return 1;
j = spot - 1;
并将while do改为while循环以避免混淆。
答案 1 :(得分:0)
仅对该循环使用变量。你在循环中使用spot来做其他事情,仅用于暂时目的。
然后,你知道do / while和while之间的差异吗?我不太了解你的程序,但如果你正在阅读一个点,你可以在开始和循环外读取它,然后用while循环检查它。它一样,但也许对你来说更容易。
答案 2 :(得分:0)
为什么在while循环中使用spot。根据C语言,它存储垃圾值。
即使您使用,也请不要为其设置值。请检查基本的C变量声明。