如果用户输入的 java 中的数据类型不正确,则给出错误消息并重新启动循环,如果是则继续?
Give an error message and restart loop if user input is not the correct datatype in java while continue if it is?
嘿,这是我遇到大问题的程序的一小部分。如果输入不是整数,我希望程序给用户一条错误消息并从头开始循环,而如果它是整数,我希望它继续进入 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;
}
}
}
您可以添加一个 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);
这样它会循环直到你得到一个有效的号码。
编辑
如果您想再次打印菜单,那么您可以这样做:在您的原始代码中,将 break 语句更改为 continue 语句,并包括对 nextLine
方法的调用 to consume the remaining \n :
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();
}
不要在不确定时获取整数并期待整数,只需获取下一行的任何内容并确定输入的值是否为具有像这样的正则表达式的整数
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
}
}
嘿,这是我遇到大问题的程序的一小部分。如果输入不是整数,我希望程序给用户一条错误消息并从头开始循环,而如果它是整数,我希望它继续进入 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;
}
}
}
您可以添加一个 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);
这样它会循环直到你得到一个有效的号码。
编辑
如果您想再次打印菜单,那么您可以这样做:在您的原始代码中,将 break 语句更改为 continue 语句,并包括对 nextLine
方法的调用 to consume the remaining \n :
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();
}
不要在不确定时获取整数并期待整数,只需获取下一行的任何内容并确定输入的值是否为具有像这样的正则表达式的整数
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
}
}