【问题标题】:Why does it prompt me twice in a row?为什么它连续提示我两次?
【发布时间】:2014-06-05 22:24:00
【问题描述】:

这个 while 循环假设会提示输入价格和 y/n 并在价格 = 0 时结束。但是,当我运行代码时,它会询问价格,接受价格,然后转到空白行,然后我在问我下一个问题之前必须再次输入号码。对于第二个问题,我只需要输入一次。

而当我打印价格数组时,值就是我第二次输入的数字。

    int keepGoing = 1;
    while (keepGoing > 0) {

        System.out.print("How much is the item? (If no more items, enter '0') ");
        if (in.nextDouble() > 0) {
            prices.add(in.nextDouble());

            System.out.print("Is the item a pet? (Y or N) ");
            String input = in.next();
            if (new String("Y").equals(input) || new String("y").equals(input)) {
                isPet.add(true);
            }
            else { isPet.add(false); }
        }
        else { keepGoing = 0; }
    }

请帮忙?

【问题讨论】:

    标签: java arraylist while-loop java.util.scanner prompt


    【解决方案1】:

    这是因为每次您写in.nextDouble() 时,用户都需要在扫描仪中输入一些内容。相反,您应该将输入存储在临时变量中:

        Double input = in.nextDouble(); // Keep the input in this variable
        if (input > 0) {                // You can use it on each of these lines
            prices.add(input);          // so that the user doesn't have to type it twice.
    
            System.out.print("Is the item a pet? (Y or N) ");
            String input = in.next();
            if (new String("Y").equals(input) || new String("y").equals(input)) {
                isPet.add(true);
            }
            else { isPet.add(false); }
        }
        else { keepGoing = 0; }
    

    附注:keepGoing 应该是 boolean 而不是 int

    另外,你可以使用new String("Y").equalsIgnoreCase(input),这样你就不需要||

    【讨论】:

      【解决方案2】:

      它询问您两次,因为您两次调用 in.nextDouble() 方法,一次在 if 语句中,另一次在下一行中。

      【讨论】:

        【解决方案3】:

        看看下面代码中的 cmets:

        int keepGoing = 1;
        while (keepGoing > 0) {
        
            System.out.print("How much is the item? (If no more items, enter '0') ");
            if (in.nextDouble() > 0) { // <-- You are asking for the input here 
                prices.add(in.nextDouble()); // <-- and asking for the input here again.
        
                System.out.print("Is the item a pet? (Y or N) ");
                String input = in.next();
                if (new String("Y").equals(input) || new String("y").equals(input)) {
                    isPet.add(true);
                }
                else { isPet.add(false); }
            }
            else { keepGoing = 0; }
        }
        

        只需将您的代码更改为这样:

        int keepGoing = 1;
        double d = 0;
        while (keepGoing > 0) {
        
            System.out.print("How much is the item? (If no more items, enter '0') ");
        
            d = in.nextDouble();
        
            if (d > 0) {
        
                prices.add(d);
        
                System.out.print("Is the item a pet? (Y or N) ");
                String input = in.next();
                if (new String("Y").equals(input) || new String("y").equals(input)) {
                    isPet.add(true);
                }
                else { isPet.add(false); }
            }
            else { keepGoing = 0; }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-06-13
          相关资源
          最近更新 更多