如何使用户无法输入任何内容,但可以按Enter进入程序的下一步?

时间:2019-04-23 01:58:13

标签: java input terminal java.util.scanner

我想制作一个程序,其中用户无法输入任何内容,但用户可以按Enter进行操作。另外,它不显示输入。它应该是这样的:

System.out.println("Welcome to Earth. Press enter to continue.");
//The code that doesn't accept input, except enter
System.out.println("Oops. Program has ended. Please wait for 100 years. Thank you!");

如果我的字符串等于“”,我尝试通过仅使程序“继续前进”来完成此操作。


public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String input;
        int x = 0;
        while(x == 0) {
        System.out.println("Welcome to Earth. press enter to continue.");
        input = scan.nextLine();
        if(input.equals("")) {
            x = 1;
        }
        if(x == 1) {
        System.out.println("OOps. Program has ended. Please wait for 100 years. Thank you!");
        }
        }
    }

结果仅为“欢迎来到地球。请按Enter继续”。如果input不等于“”,则重复一遍。

0 个答案:

没有答案