0

我正在开发一个项目(游戏),其中一个要求是如果他们输入超出范围(1-8)的选项并且输入字符,则发出警告。如果用户输入了一个无效的数字,菜单就会出现并再次询问他们想要哪个选项。如果他们输入一个字符,程序应该要求他们输入一个整数并再次要求输入。这就是我到目前为止所拥有的。它正确识别超出范围的数字并调用菜单。它还标识一个字符(无效输入),但会为用户打开输入以输入正确的选项。如何检查这两个条件?

谢谢,泰勒

            int userChoice = scnr.nextInt();//<-- use this variable
            if (userChoice.hasNextInt() == false)
            {
                System.out.println("Error: Menu selection must be an integer! Please try again:");
            }
            // Variable used for storing what the user's main menu choice
            if (0 > userChoice || userChoice > 8)
            {
                System.out.println("Error: Invalid Menu Selection.");
                System.out.println("");
                System.out.println("Available Actions:");
                System.out.println("(1) Print Market Prices");
                System.out.println("(2) Print Detailed Statistics");
                System.out.println("(3) Buy Some Sheep");
                System.out.println("(4) Buy a Guard Dog");
                System.out.println("(5) Sell a Sheep");
                System.out.println("(6) Sell a Guard Dog");
                System.out.println("(7) Enter Night Phase");
                System.out.println("(8) Quit");
                System.out.println("What would you like to do?");
                userChoice = scnr.nextInt();
            }
4

5 回答 5

0

最好在循环中使用 Switch-Case 语句。Java JDK 1.7 现在也支持 switch-case 中的“字符串”。

于 2013-10-02T04:31:03.160 回答
0

首先要对序列进行更正nextInt()hasNextInt()调用。第一个用于从输入中读取值,第二个用于查看值类型是否为int. 所以你必须先调用hasNext[Type]()`next[Type]。

其次,因为nextInt()返回的是一个int,所以hasNextInt()userChoice变量上调用是不正确的。

让我们先纠正这两个,如下所示。

if (scnr.hasNextInt()) {
    int userChoice =  scnr.nextInt();
} else {
    // input is not an int
}

现在让我们更正您的代码以获得有效的int,同时打印说明并再次要求输入无效输入。

Scanner scnr = new Scanner(System.in);

boolean incorrectInput = true;
int userChoice = -1;

while (incorrectInput) {

    if (scnr.hasNextInt()) {

        userChoice = scnr.nextInt();
        // Variable used for storing what the user's main menu choice

        if (0 >= userChoice || userChoice > 8) {
            System.out.println("Error: Invalid Menu Selection.");
            System.out.println("");
            System.out.println("Available Actions:");
            System.out.println("(1) Print Market Prices");
            System.out.println("(2) Print Detailed Statistics");
            System.out.println("(3) Buy Some Sheep");
            System.out.println("(4) Buy a Guard Dog");
            System.out.println("(5) Sell a Sheep");
            System.out.println("(6) Sell a Guard Dog");
            System.out.println("(7) Enter Night Phase");
            System.out.println("(8) Quit");
            System.out.println("What would you like to do?");

        } else {
            incorrectInput = false;
        }
    } else {
        scnr.next();
        System.out.println("Error: Menu selection must be an integer! Please try again:");
    }
}
System.out.println("userChoice = " + userChoice);
于 2013-11-06T05:32:38.070 回答
0
try{
   int userChoice = scnr.nextInt();
   if(userChoice > 0 && userChoice <9){
       // your logic
   }else{
       System.out.println("Invalid choice");
       showMenu();
   }

}catch(Exception e){
   System.out.println("Invalid choice");
   showMenu();
}

public void showMenu(){
     System.out.println("Available Actions:");
     System.out.println("(1) Print Market Prices");
     System.out.println("(2) Print Detailed Statistics");
     System.out.println("(3) Buy Some Sheep");
     System.out.println("(4) Buy a Guard Dog");
     System.out.println("(5) Sell a Sheep");
     System.out.println("(6) Sell a Guard Dog");
     System.out.println("(7) Enter Night Phase");
     System.out.println("(8) Quit");
     System.out.println("What would you like to do?");
}
于 2013-10-02T04:49:48.680 回答
0

您正在使用hasNextInt()原始方法int来查看输入是否为整数。而是使用这个:

int userChoice ;
        try{
        userChoice =scnr.nextInt();//<-- use this variable
        }
        catch(InputMismatchException ime){
            System.out.println("Error: Menu selection must be an integer! Please try again:");
        }

同样在 if 条件中使用相同的逻辑

于 2013-10-02T03:30:15.683 回答
0

或者,您可以使用强大IntegerValidatorApache Commons Validator

if (new IntegerValidator().isInRange(Integer value, int min, int max)) {
    // value is in range ...
}
于 2016-02-19T21:35:44.510 回答