在我的代码(如下)中,只是跳过了 input.next();
。有人可以指出原因吗?
for (int i=0; i<empNum; i++)//for each employee they want to work with
{
System.out.print("\r\n\r\nPROFILE FOR EMPLOYEE #" + (i+1) + ":\r\n"
+"type Hourly(1), Salaried(2), Salaried plus Commission(3)\r\n"
+"Enter 1, 2, or 3 ==> ");//display type gathering
int typeChooser = input.nextInt();//gather type
System.out.print("Name ==> ");//ask for name
String name = input.next();//get name
System.out.print("Social Security Number ==> ");//ask for ssn
String ssn = input.next();//THIS PART IS SKIPPED
System.out.print("Birthday Month (1-12) ==> ");//ask for bdayMonth
int bdayMonth = input.nextInt();//get bdayMonth
System.out.print("Birthday bonus week (1-4) ==> ");//ask for bdayWeek
int bdayWeek = input.nextInt();//get bdayWeek
}
编辑:我刚刚注意到它被跳过的唯一一次是当名称中有空格时(即 Bob Smith 而不仅仅是 Bob)
最佳答案
社会安全号码是否包含空格?如果是这样,您可以尝试 nextLine(); 方法。此方法返回当前行的其余部分,不包括末尾的任何行分隔符。
System.out.print("Social Security Number ==> ");//ask for ssn
String ssn = input.nextLine();
关于java - 为什么有时会跳过 input.next() ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31236147/