我的代码产生了意想不到的结果。似乎我的 for 循环跳过了第一次迭代,我不明白为什么。
public static void main(String[] args) {
Scanner get = new Scanner(System.in);
int number;
// Ex.1.
String family_name;
String[] family_array;
System.out.println("Enter number of family members: ");
number = get.nextInt();
family_array = new String[number];
for(int i = 0; i < number; i++){
System.out.println("Enter family member name: ");
family_name = get.nextLine();
family_array[i] = family_name;
}
for(int i = 0; i < number; i++){
System.out.println(family_array[i]);
}
}
返回...(假设输入的数字是名称)
Enter number of family members:
5
Enter family member name:
Enter family member name:
1
Enter family member name:
2
Enter family member name:
3
Enter family member name:
4
1
2
3
4
为什么第一个 get.nextLine() 被跳过?