我正在开发一个项目(一个游戏),其中一个要求就是如果他们输入一个超出范围(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();
}
答案 0 :(得分: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条件中也使用相同的逻辑
答案 1 :(得分:0)
最好在循环中使用Switch-Case语句。 Java JDK 1.7现在也支持switch-case中的'string'。
答案 2 :(得分: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?");
}
答案 3 :(得分: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);
答案 4 :(得分:0)
或者,您可以使用 Apache Commons Validator 中强大的IntegerValidator
:
if (new IntegerValidator().isInRange(Integer value, int min, int max)) {
// value is in range ...
}