我正在尝试使用主要是数组来编写此代码。我试图在每个问题之后显示“正确”或“不正确”,并在最后得到正确/不正确的答案。目标是使用数组来完成此任务,但我不知道如何使用数组在每个问题后显示正确/错误。
//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 + ".");
任何帮助表示赞赏,谢谢。