谁能帮我把这个 do-while 循环转换成 while 循环?我已经尝试过,但似乎无法在没有很多错误的情况下进行切换。谢谢!
public static void main(String args[]) {
String ch;
TicTacToe Toe = new TicTacToe();
do {
Toe.newBoard();
Toe.play();
System.out.println("Would you like to play again (Enter 'yes')? ");
Scanner in = new Scanner(System.in);
ch = in.nextLine();
System.out.println("ch value is " + ch);
} while (ch.equals("yes"));
}
以下是控制台中井字游戏的完整代码: 导入 java.util.Scanner;
public class TicTacToe {
private int tic;
private char tac[] = new char[10];
private char player;
public static void main(String args[]) {
String ch;
TicTacToe Toe = new TicTacToe();
do {
Toe.newBoard();
Toe.play();
System.out.println("Would you like to play again (Enter 'yes')? ");
Scanner in = new Scanner(System.in);
ch = in.nextLine();
System.out.println("ch value is " + ch);
} while (ch.equals("yes"));
}
public void newBoard() {
char posndef[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
int i;
tic = 0;
player = 'X';
for (i = 1; i < 10; i++)
tac[i] = posndef[i];
currentBoard();
}
public String currentBoard() {
System.out.println(" \t\t | | ");
System.out.println("\t\t" + " " + tac[1] + " | " + tac[2] + " | " + tac[3]);
System.out.println(" \t\t ____|____|____ ");
System.out.println(" \t\t | | ");
System.out.println("\t\t" + " " + tac[4] + " | " + tac[5] + " | " + tac[6]);
System.out.println(" \t\t ____|____|____ ");
System.out.println(" \t\t | | ");
System.out.println("\t\t" + " " + tac[7] + " | " + tac[8] + " | " + tac[9]);
System.out.println(" \t\t | | ");
return "currentBoard";
}
public void play() {
int spot;
char blank = ' ';
System.out.println("Player " + getPlayer() + " will go first and be the letter 'X'");
do {
System.out.println("\n\n Player " + getPlayer() + " choose a position.");
boolean posTaken = true;
while (posTaken) {
Scanner in = new Scanner(System.in);
spot = in.nextInt();
posTaken = checkPosn(spot);
if (posTaken == false)
tac[spot] = getPlayer();
}
System.out.println("Nice move.");
currentBoard();
nextPlayer();
} while (checkWinner() == blank);
}
public char checkWinner() {
char Winner = ' ';
if (tac[1] == 'X' && tac[2] == 'X' && tac[3] == 'X')
Winner = 'X';
if (tac[4] == 'X' && tac[5] == 'X' && tac[6] == 'X')
Winner = 'X';
if (tac[7] == 'X' && tac[8] == 'X' && tac[9] == 'X')
Winner = 'X';
if (tac[1] == 'X' && tac[4] == 'X' && tac[7] == 'X')
Winner = 'X';
if (tac[2] == 'X' && tac[5] == 'X' && tac[8] == 'X')
Winner = 'X';
if (tac[3] == 'X' && tac[6] == 'X' && tac[9] == 'X')
Winner = 'X';
if (tac[1] == 'X' && tac[5] == 'X' && tac[9] == 'X')
Winner = 'X';
if (tac[3] == 'X' && tac[5] == 'X' && tac[7] == 'X')
Winner = 'X';
if (Winner == 'X') {
System.out.println("Player1 wins the game.");
return Winner;
}
if (tac[1] == 'O' && tac[2] == 'O' && tac[3] == 'O')
Winner = 'O';
if (tac[4] == 'O' && tac[5] == 'O' && tac[6] == 'O')
Winner = 'O';
if (tac[7] == 'O' && tac[8] == 'O' && tac[9] == 'O')
Winner = 'O';
if (tac[1] == 'O' && tac[4] == 'O' && tac[7] == 'O')
Winner = 'O';
if (tac[2] == 'O' && tac[5] == 'O' && tac[8] == 'O')
Winner = 'O';
if (tac[3] == 'O' && tac[6] == 'O' && tac[9] == 'O')
Winner = 'O';
if (tac[1] == 'O' && tac[5] == 'O' && tac[9] == 'O')
Winner = 'O';
if (tac[3] == 'O' && tac[5] == 'O' && tac[7] == 'O')
Winner = 'O';
if (Winner == 'O') {
System.out.println("Player2 wins the game.");
return Winner;
}
// check for Tie
for (int i = 1; i < 10; i++) {
if (tac[i] == 'X' || tac[i] == 'O') {
if (i == 9) {
char Draw = 'D';
System.out.println(" Tied Game ");
return Draw;
}
continue;
} else
break;
}
return Winner;
}
public boolean checkPosn(int spot) {
if (tac[spot] == 'X' || tac[spot] == 'O') {
System.out.println("That position is already taken, please choose another");
return true;
} else {
return false;
}
}
public void nextPlayer() {
if (player == 'X')
player = 'O';
else
player = 'X';
}
public String getTitle() {
return "Tic Tac Toe";
}
public char getPlayer() {
return player;
}
}
最佳答案
不确定为什么要这样做,但它就像在到达循环时使 while
条件通过一样简单:
String ch = "yes";
TicTacToe Toe = new TicTacToe();
while("yes".equalsIgnoreCase(ch)) {
Toe.newBoard();
Toe.play();
System.out.println("Would you like to play again (Enter 'yes')? ");
Scanner in = new Scanner(System.in);
ch = in.nextLine();
}
关于java - 在井字游戏中将 do while 循环更改为 while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37417497/