字符串数组和 BufferedReader

String Arrays and BufferedReader

我正在为 class 做作业,但我卡在了最开始的地方。我不确定如何处理用户输入,我会在告诉你作业是什么后详细说明....

第一个输入将是包含十个 T 或 F 答案的测验的答案键。然后它需要用户输入学生的名字和姓氏、ID #,然后回答 T/F 的 "quiz",用户输入任意数量的学生,然后 "ZZZZ" 终止.所有用户输入都在一个条目中输入,这就是我遇到问题的地方。

程序的示例输入:

1 T T T T T F T T F F
Bobb, Bill 123456789  T T T T T F T T F F
Lou, Mary  974387643  F T T T T F T T F F
Bobb, Sam  213458679  F T F T f t 6 T F f
Bobb, Joe  315274986  t t t t t f t t f f
ZZZZ

这将产生输出: 测验 1 的结果:

123-45-6789  Bill Bobb 10
974-38-7643  Mary  Lou 9
213-45-8679  Sam  Bobb 5
315-27-4986  Joe  Bobb 10

The average score is 8.5

我们必须使用 BufferedReader 并且一次输入所有输入。我遇到的问题是我不知道如何进行输入。起初我想我会按换行符拆分输入并创建一个数组,其中每个索引都是换行符,但我现在只打印 "ZZZZ" 我不知道为什么?我也不知道如何将第一个索引(答案键)与所有学生的答案进行比较。一旦我按换行符拆分输入,然后我可以按 space 拆分该数组中的每个索引吗?任何帮助是极大的赞赏!请记住我是 Java.

的新手

到目前为止我有什么(我知道它不多,但我只是被困在前面)......

public class CST200_Lab4 {

public static void main(String[] args) throws IOException {

    String inputValue = " ";
    String inputArr[] = new String[13];
    String answerKey = null;
    String numStudents[];

    InputStreamReader ISR = new InputStreamReader(System.in);
    BufferedReader BR = new BufferedReader(ISR);


    while(!(inputValue.equalsIgnoreCase("ZZZZ"))) {
        inputValue = BR.readLine();
        inputArr = inputValue.split("\r+");
        answerKey = inputArr[0];        
    }
    System.out.println(answerKey);      
}

}

我刚刚在这里输入了这段代码,所以可能有错别字,你应该验证它。但这是一种方法。

如果使用 ArrayList 存储学生详细信息,则不需要知道学生的数量。 ArrayList 的大小是动态的。

http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

import java.util.ArrayList;

public class CST200_Lab4 {

public static void main(String[] args) throws IOException {

    String inputValue = "";
    ArrayList<String[]> students = new ArrayList<String[]>()
    String[] answerdarr;

    BufferedReader BR = new BufferedReader(new InputStreamReader(System.in));

    //first line is always the answers
    String answers = BR.readLine();
    answerArr = answers.split(" ");

    while(!(inputValue.equalsIgnoreCase("ZZZZ"))) {
        inputValue = BR.readLine();

        //add extra array element so we can later use it to store score
        inputValue = inputValue + " Score";

        String[] inputArr = inputValue.split(" ");

        int studentTotal = 0;

        for(int i = 3, i < inputArr.length; i++) {

            if(inputArr[i].equals["T"]) {

            studentTotal++; 
        }
    }

    //previous value of this is "Score" as we set earlier
    inputArr[13] = studentTotal;
    students.add(inputArr);
    }

    //Now do your printing here...
  }
}

在 main() 中使用此代码

    String inputValue = " ";
    String inputArr[] = new String[13];
    String answerKey = null;
    String numStudents[];

    InputStreamReader ISR = new InputStreamReader(System.in);
    BufferedReader BR = new BufferedReader(ISR);

    try {
        inputValue = BR.readLine();
        String answers[] = inputValue.split(" "); 
        int count = 0;
        System.out.println();
        while((inputValue = BR.readLine()) != null) {
            if (inputValue.equalsIgnoreCase("ZZZZ"))
                break;
            inputArr = inputValue.split("\s+");
            System.out.print(inputArr[2] + " ");
            System.out.print(inputArr[1] + " ");
            System.out.print(inputArr[0].split(",")[0] + " ");
            count = 0;
            for(int i = 0; i <10; i++){
                if(inputArr[i+3].equalsIgnoreCase(answers[i+1]))
                    count ++;
            }
            System.out.print(count);
            System.out.println();
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }      

}

平均的部分留给你计算了。不要提。