在打印错误消息后,在得到有效输入之前,我如何将用户困在循环中



我想向用户请求一个输入,如果它无效,我将推出一个错误语句,并要求用户输入另一个输入。我该如何编写一个循环,让用户在收到错误后保持相同的输入请求?我以前使用的方法是在Python中尝试和除外。Java中的等价物是什么?

import java.util.Scanner;
import java.util.Random;
public class Main {
//Setting Moves List
enum Move {
ROCK,
PAPER,
SCISSORS
}
//Initiate Scanner and accept user input
public static String getUserMove() {
Scanner keyboard = new Scanner(System.in);
String gesture = keyboard.nextLine();
String userMove = gesture.toUpperCase();
if (userMove.equals("ROCK") || userMove.equals("PAPER") || userMove.equals("SCISSORS")) {
return userMove;
} else {
System.out.println("This is not a valid input." +
"Please try again!");
return "Invalid Input";
}
}
//Get random input as CPU move
public static String getAiMove() {
String Ai;
Random random = new Random();
int input = random.nextInt(3);
if (input == 1) {
Ai = Move.ROCK.name();
} else if (input == 2) {
Ai = Move.PAPER.name();
} else {
Ai = Move.SCISSORS.name();
}
return Ai;
}
//Determine Winner Result
public static void main(String args[]) {
System.out.println("Choose your move against the CPU:");
System.out.println("ROCK, PAPER, OR  SCISSORS");
String playerMove = getUserMove();
System.out.println("You threw: " + playerMove);
if (!playerMove.equals("Invalid Input")) {
String cpuMove = getAiMove();
System.out.println("Computer threw: " + cpuMove);
if (playerMove.equals(cpuMove)) {
System.out.println("Tie! Nobody Wins!");
}
// Player is Rock
else if (playerMove.equals(Move.ROCK.name())) {
if (cpuMove.equals(Move.PAPER.name())) {
System.out.println("You Lost! nPaper beats Rock!");
} else {
System.out.println("You Win! nRock beats Scissors!");
}
}
// Player is Paper
else if (playerMove.equals(Move.PAPER.name())) {
if (cpuMove.equals(Move.SCISSORS.name())) {
System.out.println("You Lost! nScissors beats Paper!");
} else {
System.out.println("You Win! nPaper beats Rock!");
}
}
// Player is Scissors
else {
if (cpuMove.equals(Move.ROCK.name())) {
System.out.println("You Lost! nPaper beats Rock!");
} else {
System.out.println("You Win! nRock beats Scissors !");
}
}
}
}
}

如下更改getUserMove方法:-

public static String getUserMove() {
boolean flag = true; 
Scanner keyboard = new Scanner(System.in);
String gesture = keyboard.nextLine();
//loops until the user will not provide the desired input
while (flag) {
String userMove = gesture.toUpperCase();
if (userMove.equals("ROCK") || userMove.equals("PAPER") || userMove.equals("SCISSORS")) {
gesture = userMove;
flag = false;// set flag to false once user will give valid input.
} else {
System.out.println("This is not a valid input." + "Please try again!");
keyboard = new Scanner(System.in);// get input from user incase of invalid input.
gesture = keyboard.nextLine();
}
}
keyboard.close();// close scanner(to prevent memory leak)
return gesture;
}

您可以尝试这样的操作,而不是getUserMove()中的if语句,它将强制用户输入有效的gesture

//Initiate Scanner and accept user input
public static String getUserMove() {
Scanner keyboard = new Scanner(System.in);
String gesture = keyboard.nextLine().toUpperCase();
while(!(userMove.equals("ROCK") || userMove.equals("PAPER") || userMove.equals("SCISSORS"))){
System.out.println("This is not a valid input." + "Please try again!");
gesture = keyboard.nextLine().toUpperCase();
}
return gesture;
}

最新更新