简体   繁体   中英

Writing a program that takes input from a user as an array and compares it to another array to check for correct answers using a while loop

this is probably a really newbie issue, but I can't quite find an answer that was answered without using a for loop. I have one array set as a constant which contains the correct answers to the test. The program takes in user input and then compares it to the constant array and counts the amount of correct answers. I should not have to import any libraries and should complete the method with a while loop. I want to iterate through the users input through the ANSWER constant and create a count for all of the correct answers. Here is a snippet of my class (I've excluded the method that prompts the user for answers to keep things simple).\

public class DriverExam{
public static final char[] ANSWER = {'B','D','A','A','C','A','B','A','C','D','B','C','D','A','D','C','C','B','D','A',};
private char[] driversAnswers;
InputReader reader;

public DriverExam(){
    reader = new InputReader();
    driversAnswers = new char[20];

}
public int getTotalCorrectAnswers(){
        int correct = 0;
        int index = 0;
        while (index < ANSWER.length){
            index++;
            if(driversAnswers.equals(ANSWER)){
                correct++;
            }
            System.out.println(index);
            System.out.println(correct);
        }
        return correct;

The issue is most likely to do with the if statement but I can't seem to find a way to iterate through an arrays indices and compare them to another array.

EDIT: My current method looks like this:

 public int getTotalCorrectAnswers(){
    int correctAnswer = 0;
    int index = 0;        
    while(index < ANSWER.length){
        if(ANSWER[index]==driversAnswers[index]){
            correctAnswer++; 
            index++;
        }            
        // System.out.println(index);
        System.out.println(correctAnswer);

    }
    return correctAnswer;

For a couple of attempts I was getting some actual counts happening but it appeared that it would stop the counter once it encountered a different value. But as things stand now I am stuck with a result value of 0's

as already described in the comments, you cannot compare arrays like this. driverAnswers.equals(ANSWER) only compares the references, in this case it will return a false, because the references are different. If you want to check an array for equality you can use Arrays.equals(arr1, arr2) . In this case the equals method is called on every element in the array. If the objects in the arrays override this method correctly, this method will return the desired result.

However, you did mention not to use other libraries. The simplest solution would then be to iterate through each element and check for equality

EDIT: because you don't need the else case i made a little editing here

while(index < ANSWER.length){
    if(driverAnswer[index] == ANSWER[index])
        correct++
    index++
}

first you to check the size of the both array must equal, if it so then you can directly use Arrays.equals method for all correct answer else you can loop through the right answer, look out the simple iteration below

int correctAnswer = 0;
        if(ANSWER.length == driversAnswers.length){
            if(Arrays.equals(ANSWER,driversAnswers))
                System.out.println("correct answer: " + driversAnswers.length);
            else{
                for (int i=0;i<ANSWER.length;i++) {
                    if(ANSWER[i]==driversAnswers[i])
                        correctAnswer++;
                }
            System.out.println("correct answer: " + correctAnswer);
            }
        }

You can use while loop there instead of for

int correctAnswer = 0,index=0;
    if(ANSWER.length == driversAnswers.length){
        if(Arrays.equals(ANSWER,driversAnswers))
            System.out.println("correct answer: " + driversAnswers.length);
        else{
            while (index<ANSWER.length) {
                if(ANSWER[index]==driversAnswers[index])
                    correctAnswer++;
                index++;
            }
        System.out.println("correct answer: " + correctAnswer);
        }
    }

USE THIS FOR YOUR REFERENCE

import java.util.Arrays; class Test{ public static void main(String[] args) { char[] ANSWER = {'B','D','A','A','C','A','B','A','C','D','B','C','D','A','D','C','C','B','D','A',}; char[] driversAnswers = {'x','D','A','A','C','A','B','A','C','D','B','C','D','A','D','C','C','B','D','A',}; int correctAnswer = 0,index=0; if(ANSWER.length == driversAnswers.length){ if(Arrays.equals(ANSWER,driversAnswers)) System.out.println("correct answer: " + driversAnswers.length); else{ while (index<ANSWER.length) { if(ANSWER[index]==driversAnswers[index]) correctAnswer++; index++; } System.out.println("correct answer: " + correctAnswer); } }
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM