【问题标题】:Duplicated prompt in Java consoleJava控制台中的重复提示
【发布时间】:2014-07-10 12:28:23
【问题描述】:

我有以下代码,但是在第一轮循环后总是显示重复的提示。如何防止提示显示两次?

public static void main(String[] args) 
{
    Scanner scn = new Scanner(System.in);
    int number = 0;
    String choice = "";

    do
    {
        System.out.print("Continue / Exit: ");
        choice = scn.nextLine().toLowerCase();

        if (choice.equals("continue"))
        {
            System.out.print("Enter a number: ");
            number = scn.nextInt();
        }   
    }while(!choice.equals("exit")); 

}

程序输出:

Continue / Exit: continue
Enter a number: 3
Continue / Exit: Continue / Exit: exit    <--- Duplicated prompt here (how to remove the duplicate?)

我怀疑这与 stringint 使用扫描仪对象有关。

【问题讨论】:

  • 用调试器单步调试你的代码,它就会变得清晰。
  • @MattBall 我已经做过了,但是调试器只能显示变量的值,但不能显示存储在字符串缓冲区中的内容。 (如果我错了,请纠正我)。似乎缓冲区没有被刷新导致重复。
  • 永远不要明确检查条件中的boolean。这是容易出错且丑陋的。 if(x = true)总是true。虽然您正确使用了比较运算符,但您应该写成while(!choice.equals("exit")).
  • @BoristheSpider 我已经编辑了代码,你能取消 -1 吗?
  • 为什么我会因为你写的代码不好而对你投反对票?这不是反对票的目的。我没有投反对票,但我怀疑有几个人认为您的问题缺乏研究工作。

标签: java console prompt


【解决方案1】:

nextInt() 不读取换行符,因此您在Scanner 缓冲区中得到一个空行(只是 int 之后的换行符)。下次使用nextLine 时会读取此信息,从而导致程序不等待用户输入。您需要这样做:

number = scn.nextInt();
scn.nextLine(); //advance past newline

或者这个:

number = Integer.parseInt(scn.nextLine()); //read whole line and parse as int

the Java docs 中了解nextIntnextLine

【讨论】:

    【解决方案2】:

    你可以用这个:-

      public static void main(String[] args)
     {
     Scanner scn = new Scanner(System.in);
     int number = 0;
     String choice = "";
     do
      {
        System.out.print("Continue / Exit: ");
        choice = scn.nextLine().toLowerCase();
        if (choice.equals("continue"))
        {
            System.out.print("Enter a number: ");
             number = Integer.parseInt(scn.nextLine()); 
        }
      }while(!choice.equals("exit"));
    
      }
    

    【讨论】:

    • 不鼓励仅使用代码的答案,因为它们没有帮助。 OP遇到了什么问题?如何修复?这些问题只能通过逐行比较来回答。因此,我认为这个答案对他没有帮助。
    【解决方案3】:

    缓冲区有换行符,您需要刷新缓冲区。试试这个:

    public static void main(String[] args)
    {
        int number = 0;
        String choice;
    
        do
        {
            Scanner scn = new Scanner(System.in);
            System.out.print("Continue / Exit: ");
            choice = scn.nextLine().toLowerCase();
    
            if (choice.equals("continue"))
            {
                System.out.print("Enter a number: ");
                number = scn.nextInt();
            }   
        }while(choice.equals("exit") == false); 
    
    }
    

    输出是:

    Continue / Exit: continue
    Enter a number: 2
    Continue / Exit: continue
    Enter a number: 4
    Continue / Exit: exit
    

    【讨论】:

    • -1 因为为每次迭代创建一个新的 Scanner 是不好的做法,并且有更简洁的方法来解决这个问题。也没有解释为什么会发生这种情况。
    • @kviiri nextLine() 阅读 int 并不是更好。为什么这是不好的做法?
    • @PeterRader,一个不必要的垃圾。此外,阅读该行要好得多,因为这是用户在这里想要做的 - 阅读一行并将其视为一个 int。
    • @kviiri 扫描仪是不必要的吗?这个答案不信任输入值。
    • @PeterRader,是的,每次迭代都不需要扫描仪。您只需要一个 Scanner 对象,不需要为每次迭代创建一个新对象。
    猜你喜欢
    • 2012-09-12
    • 2019-05-26
    • 2014-02-21
    • 1970-01-01
    • 2014-02-04
    • 1970-01-01
    • 2010-11-03
    • 2020-05-12
    • 2020-10-11
    相关资源
    最近更新 更多