【问题标题】:How do I exit a while loop by pressing enter?如何按 enter 退出 while 循环?
【发布时间】:2016-02-22 21:37:49
【问题描述】:

我试图通过按键盘上的 Enter 键来中断 while 循环。我的代码是:

package javaapplication4;
import java.util.ArrayList;
import java.util.Scanner;

public class JavaApplication4 {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        ArrayList<Double> numbers = new ArrayList( );
        while (true) {
            System.out.println("Please enter the numbers seperated by a space: ");
            numbers.add(keyboard.nextDouble());
           //want the while loop to break here by pressing "enter" after entering array values
        }
        System.out.println(numbers);
    }

【问题讨论】:

    标签: java input enter


    【解决方案1】:

    不要使用循环来获取输入,或者nextDouble。您真正想要的是一行输入,然后您将其拆分为一个双精度列表。所以使用nextLine,拆分它,解析每个项目。像这样的:

    Scanner keyboard = new Scanner(System.in);
    ArrayList<Double> numbers = new ArrayList( );
    String input = keyboard.nextLine();
    for(String item : input.split(" ")){
        numbers.add(Double.parseDouble(item));
    }
    

    这忽略了任何类型的输入验证,但它显示了一种通用方法。

    这会起作用,因为一旦你点击“enter”,它就会结束第一行,这意味着扫描器可以越过nextLine 进入你的大部分代码。由于您不再尝试阅读任何内容,因此它不会阻止等待更多输入,并且一旦完成就可以成功退出。

    【讨论】:

    • 请注意Double.parseDouble(input) 将尝试解析空行,这显然会失败。 编辑:您的编辑不会解决这个问题:P
    • @Tom 我没有看到它尝试解析空行。对于我机器上1 2 3 4 5 的输入,输出为[1.0, 2.0, 3.0, 4.0, 5.0]
    • 用空行试试 :)。我知道你说过,你忽略了检查,这就是为什么我的推荐是一个“注释”,而不是批评。
    【解决方案2】:

    我自己喜欢使用 try { ... } catch (NumberFormatException),所以当你得到一个空行(即输入)时,你的 catch 块被激活并且你已经逃脱了循环

    try {
        while (true) {
          System.out.println("Please enter the numbers seperated by a space: ");
          numbers.add(keyboard.nextDouble());
          //want the while loop to break here by pressing "enter" after entering array values
        }
    } catch (NumberFormatException ex) {}
    System.out.println(numbers);
    

    【讨论】:

    • 不管你按了多少次回车,这似乎只是在等待你输入一个双精度。它只是忽略换行符。 (另外,我认为应该是InputMismatchException,而不是NumberFormatException)。
    • @resueman “不管你按了多少次回车,这似乎一直在等你输入一个双精度。” 因为keyboard.nextDouble() 甚至都懒得启动读取直到输入令牌。并且“return”不是有效的令牌(使用扫描仪默认分隔符)。
    【解决方案3】:
    import java.util.ArrayList;
    import java.util.Scanner;
    import java.util.StringTokenizer;
    
    public class JavaApplication4 {
    
        public static void main(String[] args) {
            Scanner keyboard = new Scanner(System.in);
            ArrayList<Double> numbers = new ArrayList();
            System.out.println("Please enter the numbers seperated by a space: ");
    
            String line = keyboard.nextLine();
            StringTokenizer token = new StringTokenizer(line, " ");
            while(token.hasMoreTokens()) {
                numbers.add(Double.parseDouble(token.nextToken()));
            }
    
            System.out.println("Numbers: " + numbers);
    
        }
    

    }

    【讨论】:

    • 尽管StringTokenizer 仍然有效,但它已经过时,应该由String#spit 替换。甚至这个类的 JavaDoc 也建议这样做。
    • Tom,你是对的,String#split 是更好的方法。我想我暴露了我的年龄。 :-(
    猜你喜欢
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 2015-01-18
    • 1970-01-01
    相关资源
    最近更新 更多