Java 试着在圈内接住

Java 试着在圈内接住,java,exception-handling,while-loop,try-catch,inputmismatchexception,Java,Exception Handling,While Loop,Try Catch,Inputmismatchexception,下面的代码询问用户他/她想要多少赛车手 while (true) { // loops forever until break try { // checks code for exceptions System.out.println("How many racers should" + " participate in the race?"); amountRacers = in.nextInt(); break; // if no e

下面的代码询问用户他/她想要多少赛车手

while (true) { // loops forever until break
    try { // checks code for exceptions
        System.out.println("How many racers should" + " participate in the race?");
        amountRacers = in.nextInt();
        break; // if no exceptions breaks out of loop
    } 
    catch (InputMismatchException e) { // if an exception appears prints message below
        System.err.println("Please enter a number! " + e.getMessage());
        continue; // continues to loop if exception is found
    }
}
如果在
amoutnRacers=in.nextInt()中输入了一个数字代码脱离循环,程序的其余部分运行良好;然而,当我输入诸如“awredsf”之类的内容时,它应该捕获该异常,它确实捕获了该异常。它没有再次提示用户,而是不断循环,这对我来说毫无意义

当连续循环时,程序如下打印:

应该有多少运动员参加比赛? 应该有多少运动员参加比赛? 应该有多少运动员参加比赛? 应该有多少运动员参加比赛? 应该有多少运动员参加比赛? 应该有多少运动员参加比赛? 应该有多少参赛者参加比赛?请输入一个数字!无效的 请输入一个数字!无效的 请输入一个数字!无效的 请输入一个数字!无效的 请输入一个数字!无效的 请输入一个数字!无效的 请输入一个数字!无效的

我不明白发生了什么
amountRacers=in.nextInt()那么为什么用户不能输入一个数字呢

一旦捕获到InputMismatchException,只需添加
input.next()

catch (InputMismatchException e) { //if an exception appears prints message below
    System.err.println("Please enter a number! " + e.getMessage());
    input.next(); // clear scanner wrong input
    continue; // continues to loop if exception is found
}

您需要清除错误的输入,而扫描器不会自动清除。

您可能需要创建一个扫描器类,以便从键盘获取标准输入流。您应该在代码中的某个地方有一条语句,用于创建Scanner类的实例,如:
Scanner in=new Scanner(System.in)

因此,语句中的“in”变量是:amountRacers=in.nextInt();等待并扫描键盘输入的内容并存储。

今天我解决了这个问题:-)这是我的代码。我想我可以帮忙

public int choice () throws Exception{
    Scanner read = new Scanner(System.in));
    System.out.println("Choose the option from the upper list");
    int auxiliaryChoiceMenu = 5;
    int auxiliaryVariable = -1;
    boolean auxiliaryBoolean = false;
    while (!auxiliaryBoolean) {
        try {
            auxiliaryVariable = read.nextInt();
            read.nextLine();
        } catch (Exception e) {
            System.out.println("incorrect data, try again"+e);
            read.nextLine();
            continue;
        }

        if (auxiliaryVariable<0 || auxiliaryVariable>auxiliaryChoiceMenu){
            System.out.println("incorrect data, try again");
        } else {
            auxiliaryBoolean = true;
        }
        choiceMenu = auxiliaryVariable;

    }
        return choiceMenu;
        //choicemenu is a external variable
}
public int choice()引发异常{
扫描仪读取=新扫描仪(System.in));
System.out.println(“从上面的列表中选择选项”);
int辅助选择菜单=5;
int辅助变量=-1;
布尔辅助布尔=false;
而(!辅助工具库){
试一试{
辅助变量=read.nextInt();
read.nextLine();
}捕获(例外e){
System.out.println(“数据不正确,请重试”+e);
read.nextLine();
继续;
}
if(辅助变量辅助选项菜单){
System.out.println(“数据不正确,请重试”);
}否则{
auxiliaryBoolean=true;
}
choiceMenu=辅助变量;
}
返回选项菜单;
//choicemenu是一个外部变量
}

基本上,您会一次又一次地遇到异常。这大概是因为错误的输入仍在等待读取。我知道这是因为时间限制。为什么要继续?这只是java的一个怪癖吗?@ycomp continue在本例中不是必需的。这只是我已经实例化Scanner对象的一段代码。