我有一个基本的控制台应用程序,我只是想在 while 循环中使用一个简单的 Scanner 来捕获用户输入。我试图把standardInput = System.in在build.gradle,但我的程序不阻止时,应等待输入。
while (stillWriting) {
System.out.println(
"Press 1 to write something,\n" +
"Press 2 to erase something,\n" +
"Press 3 to exit."
);
//if you select 1, it goes into the case, but does not block
//then ends up here again.
int userSelection = scanner.nextInt();
switch (userSelection) {
case 1:
System.out.println("Enter text to be written: ");
//vvvvvv should block here but doesn't. vvvvvv
String textToWrite = scanner.nextLine();
paper = pencil.write(paper, textToWrite);
break;
case 2:
//some code
break;
case 3:
stillWriting = false;
break;
default:
System.out.println(userSelection + " is not valid.");
break;
}
}
是否有任何替代品或罐子可以使用,使其正确阻塞并实际等待输入?
慕虎7371278
海绵宝宝撒
相关分类