简单 Java 方法 returns 0 不管用户输入

Simple Java method returns 0 no matter the user input

此方法应该 return 用户输入的整数,只要它只是一个整数(不是字符串、浮点数等)并且只要该整数是选项之一在给定的选项列表中。每当我向用户提供他们需要选择的选项列表时,我都想在整个程序中使用此方法。这些列表将具有不同的大小,因此我将用户可能选择的最大值 (maxValue) 作为参数传递,从而为该方法提供列表的大小。

//This method handles the players input and checks if the number entered is one of the options listed or not

public static int response(int maxValue){ //the parameter is the number of options in the particular list

    response = new Scanner(System.in);
    Boolean check = true;

    while(check){
        try{
            int yesOrNo = response.nextInt();
            if(yesOrNo > maxValue || yesOrNo <= 0){ //checks if the int entered does not exceed the list size or go below zero
                System.out.println("I'm sorry, that number was not one of the options. Please reselect your choice.");


            }else{
                check = false;
            }
        }catch(Exception e){ //catches an exception when the user enters a string or anything but an int
            System.out.println("Please only use digits to make a selection.");
            response(maxValue);
        }
    }

    return yesOrNo; //returns the user's response. Well, it is supposed to.

}

我是编程方面的初学者。我正在通过在线教程和我制作的愚蠢的小程序的反复试验来学习 Java。我正在做一个有趣的小文字冒险,但仍处于起步阶段。

我遇到的麻烦是因为这个方法只会return0。yesOrNo不是被分配了用户通过扫描仪输入的整数response吗?为什么只有 returning 0?


感谢您的回复。我现在明白我需要在 try 之外声明我的 int yesOrNo 因为它超出了范围,正如你们所说的那样,在范围内声明。

但提到了一些 'there is a completely unnecessary function call in the catch block'。唯一的问题是,如果我删除它,当用户输入字符串或其他非整数值时,System.out.println("Please only use digits to make your selection.") 会创建一个无限循环。

这是我更新的代码:

//This method handles the players input and checks if the number entered is one of the options listed or not
public static int response(int maxValue){ //the parameter is the number of options in the particular list
    response = new Scanner(System.in);
    Boolean check = true;
    int yesOrNo = 0;

    while(check){
        try{
            yesOrNo = response.nextInt();
            if(yesOrNo > maxValue || yesOrNo <= 0){ //checks if the int entered does not exceed the list size or go below zero
                System.out.println("I'm sorry, that number was not one of the options. Please reselect your choice.");


            }else{
                check = false;
            }
        }catch(Exception e){ //catches an exception when the user enters a string or anything but an int
            System.out.println("Please only use digits to make a selection.");
            response(maxValue);
        }
    }

    return yesOrNo; //returns the user's response. Well, it is supposed to.

}

在问另一个问题之前阅读其他 post 后,我发现许多其他人也面临同样的问题。某些人所说的无限循环的创建是正确的,因为当扫描器遇到错误时,它不会删除该错误的标记,从而导致 while 循环无限地再次读取相同的错误。这是我读到的内容:

“根据扫描仪的 javadoc:

'When a scanner throws an InputMismatchException, the scanner will not pass the token that caused the exception, so that it may be retrieved or skipped via some other method.'

这意味着如果下一个标记不是 int,它会抛出 InputMismatchException,但标记会保留在那里。因此在循环的下一次迭代中,getAnswer.nextInt() 再次读取相同的标记并再次抛出异常。你需要的是用完它。在 catch 中添加一个 getAnswer.next() 来消耗令牌,该令牌无效,需要丢弃。"

现在无限循环问题已解决 :) 开始寻找我还需要学习的内容。谢谢。

yesOrNo 您返回的与

不同
int yesOrNo = response.nextInt();

在你的循环中。循环中的 yesOrNo 在 try.

的结束 } 处消失(超出范围)

某处一定还有另一个 int yesOrNo。找找看。

yesOrNo 超出范围,因为您在 try 块中声明了它。当您 return 声明时,将声明移到它在范围内的位置。

Boolean check = true;
int yesOrNo;

唯一可以编译的是 yesOrNo 声明为 class 或实例变量,在此代码片段中看不到。

我们确实看到 yesOrNo 的声明是在 try 块内声明的,隐藏了返回的 yesOrNo。但是它没有理由成为 class 或实例变量。

从 class 声明中删除它,并在 try 块之前在本地声明它,因此返回时它在范围内。

int yesOrNo = 0;
try{
    yesOrNo = response.nextInt();

我在 while 循环中看到 "int yesOrNo"。 while 循环范围内读取的值仅限于此。在外部声明该变量并尝试。