【发布时间】:2016-12-03 01:57:49
【问题描述】:
我一直在用 Java 开发一个小型的剪刀石头布游戏,其中输赢的条件取决于计算机或玩家是否赢得了五场比赛。如果输入错误,我不确定如何让程序在用户输入处循环。
这是我遇到此问题的代码
我要循环的部分是带有“else if (determination.equals("Y")) {"的部分
import java.util.*;
public class Rock_Paper_Scissors {
public static void main(String arg[]) {
boolean loopGameStart = true;
while (loopGameStart) {
System.out.println("Welcome to Rock Paper Scissors, a game programmed "
+ "by Daniel Park. Would you like to start? (Y/N)");
Scanner userInput = new Scanner(System.in);
String determination = userInput.next();
if (determination.equals("N")) {
System.out.println("Please, do reconsider...");
loopGameStart = true;
} else if (determination.equals("Y")) {
Random rand = new Random();
int n = rand.nextInt(3) + 1;
// 1 = Rock, 2 = Paper, 3= Scissor
int humanWinCount = humanWinCount();
int computerWinCount = computerWinCount();
System.out.println("Choose 0, 1, or 2 (Rock/Paper/Scissor)");
Scanner userRPS = new Scanner(System.in);
int choiceRPS = userRPS.nextInt();
while ((humanWinCount < 5) && (computerWinCount < 5)) {
if (choiceRPS == 0) {
if (n == 1) {
System.out.println("TIE!!");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
} else if (n == 2) {
System.out.println("LOSS!!");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
computerWinCount = computerWinCount + 1;
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
} else if (n == 3) {
System.out.println("WIN!!");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
humanWinCount = humanWinCount + 1;
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
} else {
System.out.println("I do not understand... Try Again.");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
}
} else if (choiceRPS == 1) {
if (n == 1) {
System.out.println("WIN!!");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
humanWinCount = humanWinCount + 1;
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
} else if (n == 2) {
System.out.println("TIE!!");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
} else if (n == 3) {
System.out.println("LOSS!!");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
computerWinCount = computerWinCount + 1;
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
} else {
System.out.println("I do not understand... Try again.");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
}
} else if (choiceRPS == 2) {
if (n == 1) {
System.out.println("LOSS");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
computerWinCount = computerWinCount + 1;
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
} else if (n == 2) {
System.out.println("WIN!!");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
humanWinCount = humanWinCount + 1;
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
} else if (n == 3) {
System.out.println("TIE!!");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
} else {
System.out.println("I do not understand... Try again.");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
}
}
}
if (humanWinCount == 5) {
System.out.println("Congratulations, you win!!");
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
System.out.println("Would you like to try again? (Y/N)");
Scanner continueOrNot = new Scanner(System.in);
String contOrNot = continueOrNot.next();
if (contOrNot.equals("Y")) {
loopGameStart = true;
} else if (contOrNot.equals("N")) {
System.out.println("Okay, goodbye!!");
loopGameStart = false;
}
}
if (computerWinCount == 5) {
System.out.println("Boohoo, you lost!!");
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
System.out.println("Would you like to try again? (Y/N)");
Scanner continueOrNot = new Scanner(System.in);
String contOrNot = continueOrNot.next();
if (contOrNot.equals("Y")) {
loopGameStart = true;
} else if (contOrNot.equals("N")) {
System.out.println("Okay, goodbye!!");
loopGameStart = false;
}
}
} else {
System.out.println("I do not understand, please try again!");
}
}
}
public static int humanWinCount() {
int x = 0;
return x;
}
public static int computerWinCount() {
int c = 0;
return c;
}
}
【问题讨论】:
-
P.S.
humanWinCount()和computerWinCount()将永远是return 0 -
您不需要创建多个扫描仪对象。只维护一个。无论如何,由于它们都在使用“System.in”,如果你关闭其中一个,它们都会被关闭。所以,最好只有一个对象,在完整的程序中使用它。
-
我建议您将一些(或者更确切地说,大部分)代码提取到方法中,这样您就不会重复自己。也可以尝试使用常量。
标签: java