2017-02-10 29 views
-3

你好,我一直在試圖做一個程序,問ASK 1)有多少學生? 2)詢問一個名字和測試標記如何使用java輸入數組

有人可以幫我解決這個問題。進出口新的編程這裏是我的代碼

Scanner input = new Scanner(System.in); 

System.out.println("How many Students in class?"); 
int n = input.nextInt(); 
System.out.println("Enter the " + n + " names: "); 

String [] names = new String[n]; 

for (int i = 0; i < names.length; i++) 
{ 
    names[i] = input.nextLine();  
} 
+2

k.joe,歡迎來到StackOverflow。我想你會得到這個問題的提示,因爲它不符合網站的精神和規則。你已經提出了這個問題,但請確保你提出你在代碼中面臨的問題/錯誤,併爲此提出解決方案。請不要指望人們爲你寫邏輯。 –

+0

歡迎,請參閱[問],你沒有解釋你的問題。 – AxelH

+1

使用next(),nextInt()或其他nextFoo()方法後,[Scanner正在跳過nextLine()](http:/ /stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) –

回答

1

這是一個「問題」與nextInt,該方法僅讀出數字,讓你打電話nextLine<enter>所以接下來的時間內,<enter>存在,它會讀取這個,所以這個值是空的。您需要先清除輸入,只需閱讀該行。

System.out.println("How many Students in class?"); 
int n = input.nextInt(); 
input.nextLine(); //will consume the \n 
相關問題