【发布时间】:2015-10-28 01:55:31
【问题描述】:
在我的程序中,我试图模拟 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);
}
【问题讨论】:
-
在某个时候
list会被填满,你只需continue那个内部循环并跳过其他所有内容。
标签: java for-loop while-loop do-while tic-tac-toe