我試圖用主要數組編寫這段代碼。我試圖在每個問題後顯示「正確」或「不正確」,並在最後得到正確/不正確的答案。目標是使用數組來完成這一任務,但我無法弄清楚如何在每個問題後使用數組來顯示正確/不正確。在java中使用數組循環使用
//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 + ".");
任何幫助表示讚賞,謝謝。
數組不顯示任何內容。 – 2012-04-09 01:31:52
我不明白這個問題。 「我不知道如何使陣列顯示正確/不正確」? – Whymarrh 2012-04-09 01:32:47
我想用數組來顯示它。 – Titan 2012-04-09 01:39:40