Kann mir jemand helfen, diese Do-While-Schleife in eine While-Schleife zu konvertieren? Ich habe es versucht, kann aber nicht ohne viele Fehler wechseln. Vielen Dank!Ändern einer Do While-Schleife zu einer While-Schleife in einem Tic Tac Toe-Spiel
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"));
}
Hier ist der vollständige Code für das Tic-Tac-Toe-Spiel in der Konsole: Import 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;
}
}
uns Zeigen Sie, was Sie versucht haben, für die 'while' Schleife. Sonst wissen wir nicht, was Sie falsch machen – Hill
public static void main (Zeichenkette args []) { \t \t String ch; \t \t TicTacToe Toe = neue TicTacToe(); \t \t während (ch.equals ("ja")) { \t \t \t Toe.newBoard(); \t \t \t Toe.play(); \t \t \t System.out.println ("Möchten Sie noch einmal spielen (Geben Sie" ja "ein)?"); \t \t \t Scanner ein = neuer Scanner (System.in); \t \t \t ch = in.nextLine(); \t \t \t System.out.println ("ch Wert ist" + ch); \t \t} \t} – Alex
Ich weiß nicht, was mit ch zu tun – Alex