可能重複:
Scanner issue when using nextLine after nextIntnextLine方法在掃描儀
爲什麼丈夫下的空sc.nextLine();
所以有必要嗎?它並不等於任何東西,當然妻子也沒有這一行......但是如果沒有它,程序就不會運行。我的書評論它跳過結尾newLine字符。但我看不出有什麼可跳過的!
public class FileTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("For your blended family, \n enter the wife's name");
String wife = sc.nextLine();
System.out.print("Enter the number of her children:");
int herkids = sc.nextInt();
System.out.print("Enter the husband's name:");
//sc.nextLine(); // Why is this line so necessary?
String husband = sc.nextLine();
System.out.print("Enter the number of his children:");
int hisKids = sc.nextInt();
System.out.println(wife + " and " + husband + " have " + (herkids + hisKids) + " children.");
}
}
@JohnWatts確定,同意... – user133466