我想創建一個程序,允許我輸入5個不同人的姓名,年齡和出生年份。但是,在我輸入for循環中的第一個名稱後,我遇到了無法輸入其他名稱的問題。這裏是我的代碼:當我運行它爲什麼我不能在for循環中第一次輸入另一個字符串?
public static void main(String[] args) {
String[] names = new String[5];
int[] s = new int[5];
Scanner keyboard = new Scanner (System.in);
for (int i = 0; i < 5; i++) {
System.out.print("Name: ");
names[i] = keyboard.nextLine();
System.out.print("Age: ");
s[i] = keyboard.nextInt();
System.out.print("Year: ");
s[i] = keyboard.nextInt();
}
}
程序工作正常,但它不會讓我我進入後先進入其他4名。這是我得到的輸出:
http://stackoverflow.com/questions/13102045/skipping-nextline-after-use-next-nextint-or-other-nextfoo-methods – Reimeus