Я пытаюсь выполнить задание для школы, и у меня возникают проблемы, если каждый элемент в двух массивах String равен. Я повсюду смотрел, и кажется, что я уже делаю это правильно, но это не так.Java, сравнивая значение двух строковых массивов
Реальное быстрое задание: пользователь вводит номер викторины и решения для викторины T/F (2 TTTF F ...), а затем вводит информацию для ученика, имя, фамилию, идентификатор студента и их ответы на эту викторину.
Если я вхожу в 1 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