我正在从事一个岩石布剪刀游戏项目,我想知道如何让用户输入同一个词的多种类型。如果用户输入"RoCk“、”RoCk“或"ROCK",我希望程序允许它作为有效输入继续进行。此外,这是我自己的第一个项目,如果你有任何建议或批评,请让我知道。我想在编程方面变得更好,并乐于接受任何建议。谢谢。
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//Displays what the game is
System.out.println("Rock! Paper! Scissors!");
String[] random = {"Rock", "Paper", "Scissors"};
//Making the computer choice random
String randomString = random[(int) (Math.random() * random.length)];
//Telling the user to chose between rock paper and scissors
System.out.println("Rock Paper or Scissors?");
//User input
String User;
//If User doesn't enter Rock, Paper, or Scissors they will get an error message and have to try again
do {
User = input.nextLine();
if (!User.equals("Rock") && !User.equals("Paper") && !User.equals("Scissors")) {
System.out.println("ERROR Please enter \"Rock\" \"Paper\" or \"Scissors\" ");
}
} while (!User.equals("Rock") && !User.equals("Paper") && !User.equals("Scissors"));
//Displays what the user chose
if (User.equals("Rock")) {
System.out.println("User has chosen: Rock!");
}
if (User.equals("Paper")) {
System.out.println("User has chosen: Paper!");
}
if (User.equals("Scissors")) {
System.out.println("User has chosen: Scissors!");
}
//Telling the user what the computer has chosen
System.out.println("Computer has chosen: " + randomString + "!");
//If the user's choice equals the computers choice the game is a tie
if (User.equals("Rock") && randomString.equals("Rock")) {
System.out.println("It is a tie!");
}
if (User.equals("Paper") && randomString.equals("Paper")) {
System.out.println("It is a tie!");
}
if (User.equals("Scissors") && randomString.equals("Scissors")) {
System.out.println("It is a tie!");
}
//Deciding who wins if both User and computer chose something different
if (User.equals("Rock") && randomString.equals("Paper")) {
System.out.println("Computer has won!");
}
if (User.equals("Rock") && randomString.equals("Scissors")) {
System.out.println("User has won!");
}
if (User.equals("Paper") && randomString.equals("Scissors")) {
System.out.println("Computer has won!");
}
if (User.equals("Paper") && randomString.equals("Rock")) {
System.out.println("User has won!");
}
if (User.equals("Scissors") && randomString.equals("Paper")) {
System.out.println("User has won!");
}
if (User.equals("Scissors") && randomString.equals("Rock")) {
System.out.println("Computer has won!");
}
}发布于 2018-05-08 14:43:54
do {
User = input.nextLine();
if (!User.equals("Rock") && !User.equals("Paper") && !User.equals("Scissors")) {
System.out.println("ERROR Please enter \"Rock\" \"Paper\" or \"Scissors\" ");
}
} while (!User.equals("Rock") && !User.equals("Paper") && !User.equals("Scissors"));你在这里一直在重复你自己。
您已经有了一个包含所有选项的数组:
String[] random = {"Rock", "Paper", "Scissors"};所以只需要使用这个:
while (true) {
User = input.nextLine();
Optional<String> r = Stream.of(random).findFirst(User::equalsIgnoreCase);
if (r.isPresent()) {
User = choice; // get in the expected case
break;
}
System.out.println("ERROR Please enter \"" + String.join("\", \"", random) + "\"");
}https://stackoverflow.com/questions/50226765
复制相似问题