嘿,这是我的计划的一小部分,我遇到了很大的问题。我希望程序给用户一个错误消息,如果输入不是整数,则从头开始循环,而我希望它继续进入switch / case,如果它是一个整数。这是我到目前为止所得到的,但它没有工作,因为我得到inputmismatchexception,如果它不正确,如果它是正确的,它不会继续进入switch / case?
public static void main(String[] args) {
ArrayList<Dog> doglist = new ArrayList<Dog>();
Scanner myscan = new Scanner(System.in);
boolean running = true;
while (running) {
System.out.println("\n************************************");
System.out.println("\nWelcome to the kennel club!");
System.out.println("\n************************************");
System.out.println("\n[1] Register new dog");
System.out.println("[2] Print out list");
System.out.println("[3] Increase age");
System.out.println("[4] Remove dog");
System.out.println("[5] Quit program");
System.out.println("\n************************************");
System.out.println("\nChoose: ");
int option = 0;
boolean inputOk = false;
do {
try {
option = myscan.nextInt();
inputOk = true;
} catch (InputMismatchException e) {
System.out.println("Option must be a number");
myscan.nextLine(); // to consume the \n that remains at the end of the line after using nextInt();
}
}
while (!inputOk);
switch (option) {
case 1:
System.out.println("Write name:");
String name = myscan.next();
System.out.println("Write race:");
String race = myscan.next();
System.out.println("Age:");
int age = myscan.nextInt();
System.out.println("Weight:");
double weight = myscan.nextDouble();
Dog dog = new Dog(name, race, age, weight);
doglist.add(dog);
break;
case 2:
System.out.println("Minimum length of tail:");
double userInput1 = myscan.nextDouble();
for (Dog d : doglist) {
if (d.getTailLength() >= userInput1) {
System.out.println(d.toString());
}
}
break;
case 3:
System.out.println("Name of dog:");
String userInput2 = myscan.next();
int flag = 0;
for (Dog d : doglist) {
if (d.getName().equals(userInput2)) {
d.increaseAge();
d.increasetailLength();
flag = 1;
break;
}
}
if (flag == 0) {
System.out.println("Error, can't find dog with name:" + userInput2);
}
break;
case 4:
System.out.println("Name of dog:");
String userInput3 = myscan.next();
Dog dogToRemove = null;
for (Dog d : doglist) {
if (d.getName().equals(userInput3)) {
dogToRemove = d;
System.out.println("Dog is removed");
}
}
if (dogToRemove == null) {
System.out.println("Error, can't find dog with name: " + userInput3);
} else {
doglist.remove(dogToRemove);
}
break;
case 5:
running = false;//Avslutar loopen och därmed programmet
System.out.println("Program finshed");
break;
default:
System.out.println("Error, choose between [1] [2] [3] [4] [5]");//Felmeddelande om valet är någon annan siffra än de som menyn innehåller
break;
}
}
}
答案 0 :(得分:2)
当你不能保证整数时,不要抓住并期望一个整数,只需抓住下一行的任何内容,并确定输入的值是否是带有正则表达式的整数
while(running)
{
inputFromUser = myscan.nextLine();
int option = -1; //some default invalid value
boolean doSwitch = false;
if(inputFromUser.matches("\\d+"))
{
option = Integer.parseInt(inputFromUser);
doSwitch = true;
}
else{
System.out.println("Incorrect input was received - " + inputFromUser);
}
if(doSwitch)
{
//switch logic
}
}
答案 1 :(得分:1)
您可以添加do-while来读取捕获InputMismatchException的选项:
int option = 0;
boolean inputOk = false;
do{
try {
option = myscan.nextInt();
inputOk = true;
} catch (InputMismatchException e) {
System.out.println("Error, this is not an integer.");
myscan.nextLine(); // to consume the \n that remains at the end of the line after using nextInt();
}
}while (!inputOk);
这样它会循环,直到你得到一个有效的数字。
修改强>
如果您想再次打印菜单,则可以执行以下操作:在原始代码中,使用continue语句更改break语句,并同时调用nextLine
方法to consume the remaining \n:< / p>
int option = 0; // don't read the value directly before validating that is actually an integer value or you will get the InputMismatchException
if (!myscan.hasNextInt()) {
System.out.println("Error, this is not an integer:");
myscan.nextLine(); // consume the \n
continue; // loop again
} else {
option = myscan.nextInt();
}