我想完成學校的任務,並且遇到問題,看看兩個字符串數組中的每個元素是否相等。我看到無處不在,似乎我已經在正確地做,但事實並非如此。Java,比較兩個字符串數組的值
真的很快,作業是用戶輸入測驗編號和T/F測驗(2 TTTF F ...)的解決方案,然後輸入信息給學生,第一個姓,學生ID以及他們對這個測驗的回答。
如果我在回答鍵中輸入了T T T F F T T和學生答案中的T T T F F F F,那麼它們就會被視爲相等,我無法弄清楚原因。我會發布我所有的代碼,但在適當的循環周圍添加星星,我認爲問題是...謝謝你的任何幫助,這讓我瘋狂!
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);
}
}
}
刪除'if'條件後面的分號。 – rgettman
經典錯字。時間來結束這個問題。 – ryanyuyu