Java 为什么我需要输入我的选择两次?
有人能帮我理解为什么程序要求我在继续之前输入两次选择吗Java 为什么我需要输入我的选择两次?,java,input,selection,Java,Input,Selection,有人能帮我理解为什么程序要求我在继续之前输入两次选择吗 public static void main(String[] args) { String quit = "quit"; String input = "input"; String print = "print"; @SuppressWarnings("resource") Scanner scan = new Scanner(System.in); System.out.prin
public static void main(String[] args) {
String quit = "quit";
String input = "input";
String print = "print";
@SuppressWarnings("resource")
Scanner scan = new Scanner(System.in);
System.out.println("please enter a command : \n 1) Quit \n 2) Input \n 3) Print \n");
String initialSelection = scan.next();
boolean stringInput = scan.hasNext();
boolean intInput = scan.hasNextInt();
boolean boolinput = scan.hasNextBoolean();
if(initialSelection.equalsIgnoreCase(quit)) {
System.out.println("The program has quit.");
System.exit(0);
}else if(initialSelection.equalsIgnoreCase(print)){
[constructor]
do {
System.out.println("\nplease enter a command : \n 1) Quit \n 2) Input \n 3) Print \n");
initialSelection = scan.nextLine();
if (initialSelection.equalsIgnoreCase(print)) {
new BonusArrayList();
} else if(initialSelection.equalsIgnoreCase(quit)) {
System.out.println("The program has quit.");
System.exit(0);
}
} while (initialSelection.equalsIgnoreCase(print));
}else if(initialSelection.equalsIgnoreCase(input)){
System.out.println("\n Please enter some kind of value (ex: 123, hello, True)");
}
}
我相信它来自嵌套的do while语句,但我似乎无法解决这个问题。任何提示都将不胜感激 这是因为你需要
String initialSelection = scan.next();
或
如果两者都有,则需要输入两次,这很有意义。非常感谢!
boolean stringInput = scan.hasNext();
boolean intInput = scan.hasNextInt();
boolean boolinput = scan.hasNextBoolean();