我使用 Scanner nextInt() 和nextLine() 来读取输入。
酱紫的:
System.out.println("Enter numerical value"); int option;
option = input.nextInt(); // Read numerical value from inputSystem.out.println("Enter 1st string"); String string1 = input.nextLine(); // Read 1st string (this is skipped)System.out.println("Enter 2nd string");String string2 = input.nextLine(); // Read 2nd string (this appears right after reading numerical value)
问题是,在输入数值之后,跳过第一个input.nextLine(),执行第二个 input.nextLine() ,所以输出如下所示:
Enter numerical value3 // This is my inputEnter 1st string // The program is supposed to stop here and wait for my input, but is skippedEnter 2nd string // ...and this line is executed and waits for my input
我测试了应用程序,好像问题在于使用input.nextInt()。 如果我删除它,那么 string1 = input.nextLine() 和string2 = input.nextLine()都会执行。