【问题标题】:Scanner inside method does not wait for answer扫描仪内部方法不等待答案
【发布时间】:2013-02-22 09:43:15
【问题描述】:

为此苦苦挣扎了一天,来回阅读论坛,没有结果。任何人都可以告诉我为什么函数 aMenu() 的第二次调用返回零并且不等待新的用户输入?我尝试了各种方法,例如 hasNextInt()、nextLine(),但没有任何效果。 hasNextInt() 在用户写东西之前不应该阻塞吗?我该如何解决这个问题?谢谢。

package FirstJavaPackage;
import java.util.Scanner;

public class testScanner
{
    public static void main(String[] args)
    {
        int choice = aMenu();
        System.out.println("You typed: "+choice);
        choice = aMenu();
        System.out.println("You typed: "+choice);
    }

    public static int aMenu()
    {
        int result = 0;
        System.out.println("In aMenu... enter an int: ");
        Scanner keyboard = new Scanner(System.in);
        if (keyboard.hasNextInt())
            result = keyboard.nextInt();
        keyboard.close();
        return result;
    }
}

输出是:

在 aMenu... 中输入一个 int: 2 您输入:2 在 aMenu... 输入一个 int: 您输入:0

【问题讨论】:

    标签: java java.util.scanner


    【解决方案1】:

    由于scanner.close 将关闭整个输入源,您应该将扫描仪传递给您的 aMenu 方法并执行以下操作:

    import java.util.Scanner;
    
    public class TestScanner
    {
       public static void main(String[] args)
       {
          Scanner keyboard = new Scanner(System.in);
          int choice = 0;
    
          do
          {
             choice = aMenu(keyboard);
             System.out.println("You typed: " + choice);
          } while (choice > 0);
    
          keyboard.close();
        }
    
        public static int aMenu(Scanner keyboard)
        {
            int result = 0;
            System.out.println("In aMenu... enter an int: ");
    
            if (keyboard.hasNextInt())
               result = keyboard.nextInt();
    
            return result;
        }
    }
    

    【讨论】:

      【解决方案2】:

      第一次调用后,您实际上关闭了System.in 输入流。

      来自Scanner.close() 文档:

      When a Scanner is closed, it will close its input source if the source 
      implements the Closeable interface.
      

      尽量不要在aMenu结尾处close你的扫描器:在aMenu方法之外初始化扫描器并让该方法使用它。

      【讨论】:

        【解决方案3】:

        您需要在对 aMenu() 的调用中重复使用相同的 Scanner 对象:

        public static void main(String[] args)
        {
            Scanner keyboard = new Scanner(System.in);
            int choice = aMenu(keyboard);
            System.out.println("You typed: "+choice);
            choice = aMenu(keyboard);
            System.out.println("You typed: "+choice);
        }
        
        public static int aMenu(Scanner keyboard)
        {
            int result = 0;
            System.out.println("In aMenu... enter an int: ");
            result = keyboard.nextInt();
            return result;
        }
        

        更多讨论请见How to use multiple Scanner objects on System.in?

        【讨论】:

        • 谢谢大家,现在它工作正常,但我仍然没有完全理解逻辑。好的,我在 aMenu() 函数结束时关闭了输入流,但是我不是在下一次调用时再次打开它,使用行 Scanner keyboard = new Scanner(System.in) 吗?我就是这么说的,我已经阅读了你推荐的链接,但我想我仍然错过了一些东西。为什么它不能那样工作,稍后关闭并重新打开流?再次感谢,您在这里建立了一个很棒的社区!
        • @xx:一方面,当您调用keyboard.close() 时,也会关闭System.in。从那时起,您将无法从System.in 中读取任何内容,无论您是否尝试创建任何其他扫描仪。
        • 谢谢,现在我明白了。因此,一旦流关闭,它在程序的其余部分保持关闭,您无法扫描不存在的内容..
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多