gpt4 book ai didi

java - 如果用户输入的数据类型不正确,则给出错误消息并重新启动循环,如果是,则继续循环?

转载 作者:行者123 更新时间:2023-12-01 20:03:17 24 4
gpt4 key购买 nike

嘿,这是我的程序的一小部分,我遇到了很大的问题。我希望程序向用户提供错误消息,如果输入不是整数,则从头开始循环,而如果输入是整数,我希望它继续进入 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;
}
}
}

最佳答案

当您无法保证整数时,不要抓取并期待一个整数,只需抓取下一行的内容并使用如下正则表达式确定输入的值是否为整数

      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
}
}

关于java - 如果用户输入的数据类型不正确,则给出错误消息并重新启动循环,如果是,则继续循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47760339/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com