如何在字符串中存储用户输入?

时间:2011-01-10 14:15:37

标签: java

如何在字符串中存储用户键盘输入中的每个答案?

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);
}

1 个答案:

答案 0 :(得分: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());
}