java - 简单的 Java 方法无论用户输入如何都返回 0

标签 java methods return-value

此方法应该返回用户输入的整数,只要它只是一个整数(不是字符串、 float 等)并且该整数是给定选项列表中的选项之一。每当我向用户提供他们需要选择的选项列表时,我想在整个程序中使用此方法。这些列表将具有不同的大小,因此我将用户可能选择的最大值 (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。我正在制作一个有趣的小文字冒险游戏,目前仍处于起步阶段。

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

<小时/>

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

但是有一些人提到“catch block 中有一个完全不必要的函数调用”。唯一的问题是,如果我删除它,当用户输入字符串或其他非字符时,会使用 System.out.println("Please only usedigits to make your choice.") 创建无限循环。整数值。

这是我更新的代码:

//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.

}
<小时/>

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

“根据扫描仪的 javadoc:

“当扫描器抛出 InputMismatchException 时,扫描器将不会传递导致异常的 token ,以便可以通过其他方法检索或跳过它。”

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

现在无限循环问题已经解决了:)继续寻找我还需要学习的东西。谢谢。

最佳答案

yesOrNo 超出了范围,因为您在 try block 中声明了它。返回声明时,将声明移至其范围内的位置。

Boolean check = true;
int yesOrNo;

关于java - 简单的 Java 方法无论用户输入如何都返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29109213/

相关文章:

javascript - 如何将 JavaScript 回调设置为外部(包装)对象的方法?

java - 在java程序关闭之前调用方法

c++ - 函数返回机制 :Temporary object, R-Value, L-Value

c - 未指定的返回值

java - 同步方法会锁定其中的所有方法吗?

java - JPA 外键 - ID 或引用

java - 使用 Maven 进行 Vaadin 项目

java - 如何在 Selenium 中测试 "Remember Me"复选框功能

java - 阴影还是实现?

c++ - 首选哪个,return const double& OR return double