2011-01-10 54 views
1

如何將用戶鍵盤輸入中的每個答案存儲在字符串中?如何在字符串中存儲用戶輸入?

for (int i = 0; i < 3; i++){ 
    System.out.println("insert the answer "); 
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 

    String userAnswer[] = new String[3]; 

    try { 
     userAnswer[i] = br.readLine(); 
    } catch (IOException ioe) { 
     System.out.println("IO error ,trying to read your answer"); 
     System.exit(1); 
    } 
    int count =0; 
    count++; 
    String we[] = new String[3]; 

    System.out.println("the answer is " + userAnswer); 
    System.out.println("the answer is " + count); 
} 
+0

這段代碼有什麼問題? – 2011-01-10 14:28:06

+0

您是否試圖接受單個輸入並將其分爲三個字符串?你想輸入三個單獨的字符串嗎?你的意圖不明確。 – Jeremy 2011-01-10 14:28:06

回答

3

使用Scanner

Scanner scanner = new Scanner(System.in); 
List<String> answers = new ArrayList<String>(); 
for(int i = 0; i < 3; i++) 
{ 
    answers.add(scanner.nextLine()); 
} 
相關問題