Java: 为什么我的程序只有运行 9 次而不是1000 次(for 和while 循环)?
Java: Why is my program only running 9 times instead of 1000 (for and while loops)?
在我的程序中,我试图模拟 1000 场随机井字游戏。游戏只玩了九次,可能是由于内部嵌套的 do-while 循环。我不确定如何解决这个问题,我尝试将内部 do-while 循环更改为 while 循环,将外部 for 循环更改为 while 循环。我知道这可能是一个简单的错误,但我无法查明错误所在。下面是我对这两个循环的代码。预先感谢您的帮助。
for (count = 0; count < 1001; count++) {
int movecount = 0;
int row, col;
int player = 1;
do {
//pick a row
row = r.nextInt(3);
//pick a col
col = r.nextInt(3);
//check if spot is empty
if (list[row][col]>0) {continue;}
//if empty, move current player there, add to count
list[row][col] = player;
if (CheckRowWin(player, list)) {
System.out.println("Player " + player + " won");
break;
} else {
System.out.println("Tie Game");
}
movecount++;
//switch player turn
player = 3 - player;
} while (movecount < 9);
}
你的外循环 是 运行 1001 次,它似乎没有,因为你的外循环除了 do{}while()
之外没有别的东西这是 运行 只有九次并打印出来的东西。
for (count = 0; count < 1001; count++) {
int movecount = 0;
int row, col;
int player = 1;
do {
//pick a row
row = r.nextInt(3);
//pick a col
col = r.nextInt(3);
//check if spot is empty
if (list[row][col]>0) {continue;}
//if empty, move current player there, add to count
list[row][col] = player;
if (CheckRowWin(player, list)) {
System.out.println("Player " + player + " won");
break;
} else {
System.out.println("Tie Game");
}
movecount++;
//switch player turn
player = 3 - player;
} while (movecount < 9);
// don't forget to reset movecount
// so that the inner loop will run again
movecount = 0;
// clear the "board" for the next game
// note: using two nested loops is slow and inefficient
// but it goes along with the theme of learning loops
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
list[r][c] = 0;
}
}
}
在我的程序中,我试图模拟 1000 场随机井字游戏。游戏只玩了九次,可能是由于内部嵌套的 do-while 循环。我不确定如何解决这个问题,我尝试将内部 do-while 循环更改为 while 循环,将外部 for 循环更改为 while 循环。我知道这可能是一个简单的错误,但我无法查明错误所在。下面是我对这两个循环的代码。预先感谢您的帮助。
for (count = 0; count < 1001; count++) {
int movecount = 0;
int row, col;
int player = 1;
do {
//pick a row
row = r.nextInt(3);
//pick a col
col = r.nextInt(3);
//check if spot is empty
if (list[row][col]>0) {continue;}
//if empty, move current player there, add to count
list[row][col] = player;
if (CheckRowWin(player, list)) {
System.out.println("Player " + player + " won");
break;
} else {
System.out.println("Tie Game");
}
movecount++;
//switch player turn
player = 3 - player;
} while (movecount < 9);
}
你的外循环 是 运行 1001 次,它似乎没有,因为你的外循环除了 do{}while()
之外没有别的东西这是 运行 只有九次并打印出来的东西。
for (count = 0; count < 1001; count++) {
int movecount = 0;
int row, col;
int player = 1;
do {
//pick a row
row = r.nextInt(3);
//pick a col
col = r.nextInt(3);
//check if spot is empty
if (list[row][col]>0) {continue;}
//if empty, move current player there, add to count
list[row][col] = player;
if (CheckRowWin(player, list)) {
System.out.println("Player " + player + " won");
break;
} else {
System.out.println("Tie Game");
}
movecount++;
//switch player turn
player = 3 - player;
} while (movecount < 9);
// don't forget to reset movecount
// so that the inner loop will run again
movecount = 0;
// clear the "board" for the next game
// note: using two nested loops is slow and inefficient
// but it goes along with the theme of learning loops
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
list[r][c] = 0;
}
}
}