开发者

How to get empty input from the user in Java

开发者 https://www.devze.com 2023-02-10 12:19 出处:网络
How do I get user input while asking the user Do you want to continue or not? If the user press开发者_运维百科 Enter then how do I go to the next instructions so I compare it to do other tasks?Scann

How do I get user input while asking the user Do you want to continue or not?

If the user press开发者_运维百科 Enter then how do I go to the next instructions so I compare it to do other tasks?


Scanner scanner = new Scanner(System.in);
scanner.nextLine();

Edit: Sorry, misread question (well question title and body is different). You can ask the user like this:

System.out.print("Do you want to continue? ");
Scanner scanner = new Scanner(System.in);

if (scanner.next().toLowerCase().equals("y")) {
    // continue...
} else {
    // don't continue
}

So when a user types in "y" or "Y" it will continue. Otherwise, it won't.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号