简体   繁体   English

当用户按下回车键时停止程序

[英]stop the program when user press enter key

Please test my code please.请测试我的代码。 I am trying to terminate my program when user press enter key or not texting anything.But my program is not terminate.Help me please!当用户按下回车键或不发短信时,我试图终止我的程序。但我的程序没有终止。请帮帮我!

Scanner s = new Scanner(System.in);
        String a =s.nextLine();
        
        while(s.hasNext())
        {
            System.out.println(s.nextLine());
        }

You want it to echo whatever the user types until they hit enter without typing anything in?您希望它回显用户输入的任何内容,直到他们在没有输入任何内容的情况下按下回车键?

Scanner s = new Scanner(System.in);
String a = s.nextLine();
while(!a.equals("")) {
    System.out.println(a);
    a = s.nextLine();
}

This thread might help you.这个线程可能会帮助你。 Java Scanner hasNext() doesn't return false Java 扫描仪 hasNext() 不返回 false

your s.hasNext() is pending in your code because the Scanner is on System.in您的 s.hasNext() 在您的代码中挂起,因为 Scanner 位于 System.in

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM