Java,比较两个字符串数组的值

时间:2015-10-07 21:58:20

标签: java arrays string equals

我正在尝试完成学校的作业,我遇到的问题是看两个String数组中的每个元素是否相等。我到处都看,似乎我已经正确地做了但事实并非如此。

真的很快,分配是用户输入测验编号和T / F测验的解决方案(2 TTTF F ...),然后输入学生的信息,姓名,学生证和他们对那个测验的回答。

如果我在答案键输入1 T T T F F T T并且为学生答案输入T T T F F F F,则将它们全部计为相等,我无法弄清楚原因。我将发布我的所有代码,但在适当的for循环周围添加星星,我认为问题是... 感谢您的帮助,这让我发疯!

public class CST200_Lab4 {

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

    String inputValue = " ";
    String answerKey [];
    String firstName = " ";
    String lastName = " ";
    String IDnumber = " ";
    int studentResults = 0;
    int numStudents = 0;
    double averageScore = 0;


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

    //Read in the Answer Key to the Quiz w/ quiz number at index 0.
    String answers = BR.readLine();
    answerKey = answers.split(" "); 
    /*Read in Students info and students quiz results.
     *Set size of student array to length of the answer key */
    String studentArr[] = new String [answerKey.length + 4];
    inputValue = BR.readLine();
    studentArr = inputValue.split("\\s+");

    //Enter students info until 'zzzz' is entered.
    while(!(inputValue.equalsIgnoreCase("ZZZZ"))) {         

        //Keep track of student(s) entered to calculate average scores.
        numStudents++;

        //quizResults = inputValue.replaceAll("^(\\S*\\s){4}", "");
        //quizResultsArr = quizResults.split("\\s+");

        //Set students info to first three indexes of studentArr.
        lastName = studentArr[0];
        firstName = studentArr[1];
        IDnumber = studentArr[2];

        /*Loop through answerKey and compare w/ student quiz results 
         *to determine how many questions the student got correct*/


    //I THINK THE ISSUE IS IN HERE BUT I CAN'T FIGURE OUT WHY 
    ******************************************************************************      
         for(int i = 1; i < studentArr.length - 2; i++) {
            //ALL 'ANSWERS' ARE BEING COUNTED AS EQUAL
            if((studentArr[i + 2]).equalsIgnoreCase(answerKey[i])); {
                studentResults++;   
                averageScore++;
            }
        }   
     ******************************************************************************

        //Print out Students info and results.
        lastName.replace(",", " ");
        System.out.print(IDnumber + "  ");
        System.out.print(firstName);
        System.out.print(lastName + " ");
        System.out.println(studentResults);
        System.out.println(averageScore);

        //Enter a new students info or 'zzzz' to end
        studentResults = 0; 
        inputValue = BR.readLine();
        studentArr = inputValue.split("\\s+");

    }
    /*If no more students are being entered, 
     *calculate average score of test results.*/
    System.out.println(numStudents);
    System.out.println(averageScore);
    if(inputValue.equalsIgnoreCase("ZZZZ")) {
        averageScore = averageScore / numStudents;
        System.out.println("The average score is " + averageScore );
    }           
}
}

1 个答案:

答案 0 :(得分:4)

if((studentArr[i + 2]).equalsIgnoreCase(answerKey[i]));

不应以;;

结束条件if语句