コーディングにはまったく新しいですが、私のコードでこの問題を見つけるのには苦労しています。それは他のいくつかのクラスを実行し、あなたが再びプレイしたいかどうか尋ねるクラスです。while文の間違った値java
問題は、最初のゲームを選択してから、もう一度再生したいと答えた後に2番目のゲームをプレイするときにnまたはいいえを再生すると、代わりに再生するゲームゲームを繰り返す。
public static void arcade() {
Scanner scanner2 = new Scanner(System.in);
String choice;
int game;
boolean finish;
finish = false;
game = 0;
do {
try {
System.out.println(
"Which game would you like to play? \n1. Coin toss\n2. Rock Paper Scissors \n3. Number Game\n4. Exit");
game = scanner2.nextInt();
switch (game) {
case 1:
choice = "yes";
do {
if (choice.equalsIgnoreCase("yes") || choice.equalsIgnoreCase("y")) {
coinToss(scanner2);
System.out.println("Would you like to play again?");
choice = scanner2.next();
} else if (choice.equalsIgnoreCase("no") || choice.equalsIgnoreCase("n")) {
System.out.println("Goodbye");
finish = true;
} else {
System.out.println("Invalid selection");
choice = scanner2.next();
}
} while (finish != true);
break;
case 2:
choice = "yes";
do {
if (choice.equalsIgnoreCase("yes") || choice.equalsIgnoreCase("y")) {
rockPaperScissors(scanner2);
System.out.println("Would you like to play again?");
choice = scanner2.next();
} else if (choice.equalsIgnoreCase("no") || choice.equalsIgnoreCase("n")) {
System.out.println("Goodbye");
finish = true;
} else {
System.out.println("Invalid selection");
}
} while (finish != true);
break;
case 3:
choice = "yes";
do {
if (choice.equalsIgnoreCase("yes") || choice.equalsIgnoreCase("y")) {
numberGame(scanner2);
System.out.println("Would you like to play again?");
} else if (choice.equalsIgnoreCase("no") || choice.equalsIgnoreCase("n")) {
System.out.println("Goodbye");
finish = true;
} else {
System.out.println("Invalid selection");
}
} while (finish != true);
break;
case 4:
System.out.println("Goodbye");
break;
default:
System.out.println("Invalid selection");
}
} catch (java.util.InputMismatchException e) {
System.err.println("Please use numbers");
scanner2.nextLine();
}
} while (game != 4);
scanner2.close();
}
応じてコードと[編集]あなたのポスト投函で[MCVE]ガイダンスをお読みください。 –
最後に質問を削除したのはなぜですか? –