因此,在 Java 中,我希望用户在我的提示中提供 Y 或 N。可以说(梦境序列以漩涡声开始)我当前正在使用此代码:
String output = "";
Scanner scan = new Scanner(System.in);
while (true) {
System.out.println("[P]ast or [F]uture?");
String input = scan.nextLine().toLowerCase();
if (input.equals("p") || input.equals("f")){
output = input;
break;
} else {
System.out.println("Please provide a valid answer.");
}
}
必须有一种更有效的方法来做到这一点。谁能帮我解答一下吗?
最佳答案
您无法强制用户输入某些内容。您必须清理数据并在用户输入错误时向用户抛出错误,就像您的代码现在所做的那样。
关于java - 强制用户提供两个答案之一?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31258284/