I’m trying to write this code using mostly arrays. I’m trying to get “Correct” or “Incorrect” to display after each question and to get a total of correct/incorrect answers at the end. The goal is to use arrays to accomplish this, but I can’t figure out how to use the array to display correct/incorrect after each question.
//Declarations
String rightAnswer[] = { "B", "C", "A" };
String userAnswer[] = new String [3];
String answer;
int counter;
int finalInt = 3;
int index = 0;
int correct = 0;
int incorrect = 0;
// detailedLoop
for(index = 0; index < finalInt; index++) {
do {
answer = JOptionPane.showInputDialog("What planet has the largest rings? \n\nA. Neptune \nB. Saturn \nC. Jupiter");
userAnswer[index] = answer;
} while(!(answer.equals("A")|| answer.equals("B") || answer.equals("C")));
index++;
do {
answer = JOptionPane.showInputDialog("What planet has a large redspot? \n\nA. Neptune \nB. Saturn \nC. Jupiter");
userAnswer[index] = answer;
} while(!(answer.equals("A")|| answer.equals("B") || answer.equals("C")));
index++;
do {
answer = JOptionPane.showInputDialog("What planet is farthest from the sun? \n\nA. Neptune \nB. Saturn \nC. Jupiter");
userAnswer[index] = answer;
} while(!(answer.equals("A")|| answer.equals("B") || answer.equals("C")));
if(userAnswer[index].equals(rightAnswer[index])) {
System.out.println("Correct!");
correct++;
index++;
}
if(!(userAnswer[index].equals(rightAnswer[index]))) {
System.out.println("Incorrect. The right answer is " + rightAnswer[index] + ".");
incorrect++;
index++;
}
}
System.out.println("Total correct:" + correct + ". Total incorrect: " + incorrect + ".");
Any help is appreciated, thanks.
This may be what you are trying to do, again, I don’t understand fully the question:
Please excuse my style.