开发者

How to get empty input from the user in Java

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜