2012-04-09 43 views
0

我試圖用主要數組編寫這段代碼。我試圖在每個問題後顯示「正確」或「不正確」,並在最後得到正確/不正確的答案。目標是使用數組來完成這一任務,但我無法弄清楚如何在每個問題後使用數組來顯示正確/不正確。在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 + "."); 

任何幫助表示讚賞,謝謝。

+0

數組不顯示任何內容。 – 2012-04-09 01:31:52

+0

我不明白這個問題。 「我不知道如何使陣列顯示正確/不正確」? – Whymarrh 2012-04-09 01:32:47

+0

我想用數組來顯示它。 – Titan 2012-04-09 01:39:40

回答

1

這可能是你正在嘗試做的,再說,我不完全理解這個問題:

String questions[] = { 
    "What planet has the largest rings? \n\nA. Neptune \nB. Saturn \nC. Jupiter", 
    "What planet has a large redspot? \n\nA. Neptune \nB. Saturn \nC. Jupiter", 
    "What planet is farthest from the sun? \n\nA. Neptune \nB. Saturn \nC. Jupiter" 
}; 
String rightAnswer[] = { "B", "C", "A" }; 
String answer = null; // the user's answer 
for (int i = 0; i < questions.length; i++) { 
    do { 
     answer = JOptionPane.showInputDialog(questions[i]); 
    } while (!(answer.equals("A")|| answer.equals("B") || answer.equals("C"))); 
    String x = (answer.equals(rightAnswer[i])) ? "correct" : "incorrect.\nThe correct answer was " + rightAnswer[i]; 
    JOptionPane.showMessageDialog(null, "Your answer was " + x + "."); 
} 

請原諒我的風格。

+0

是的!但是,不要讓它在一個盒子裏說出來,我希望它顯示「正確!」或者「錯誤,答案是」每個問題後控制檯中的+ rightAnswer [index]。 – Titan 2012-04-09 02:00:27

+0

@羅伯特更新了upvote? – Whymarrh 2012-04-09 02:03:10

+0

我會的,但我需要一個代表15投票的代表:( – Titan 2012-04-09 02:08:25

1

在循環接收到的答案之前,您絕不會將索引值重置爲0。 所以我相信你的程序試圖訪問不存在的陣列點,如3,4,5,因爲你創建了一個只有3個字符串的陣列

1 Mod對你的程序,使其更容易使用調試也將接受小寫的答案。

另外,程序不會告訴用戶他們告訴他們哪個問題是錯誤或正確的,如果發生了變化,可以幫助您瞭解程序中的問題。

+0

如果在每個問題後用戶輸入正確和不正確,我希望它顯示。我寫的代碼並沒有這樣做,它在最後做了,然後崩潰了。 – Titan 2012-04-09 01:35:20

+0

@Robert崩潰?爲什麼?什麼錯誤? – Whymarrh 2012-04-09 01:39:27

+0

@Whymarrh線程「main」中的異常java.lang.ArrayIndexOutOfBoundsException:3 – Titan 2012-04-09 01:44:45

1

本節:

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++; 
    } 

需要在某種for循環。它在技術上在for循環,但你自己增加index所以它不是有效在for循環。

,如果你做這樣的事情應該工作:

int correct = 0; 
int incorrect = 0; 
for(i = 0; i < 3; i++) { 
    if(userAnswer[i].equals(rightAnswer[i])) { 
     System.out.println("Correct!"); 
     correct++; 
    } 

    if(!(userAnswer[i].equals(rightAnswer[i]))) { 
     System.out.println("Incorrect. The right answer is " + rightAnswer[i] + "."); 
     incorrect++; 
    } 
} 

編輯

此外,作爲一般規則,你不應該增加你的計數器變量在for循環。 for-loop已經做到了。注意我的代碼中我沒有增加自己的i。每次執行for循環時,i將在i < 3評估之前遞增。

1

在收集用戶答案後,您似乎沒有重置索引,因此在您到達驗證答案索引已經爲2的塊的位置時,只驗證數組的最後一個元素你的答案。基本上所發生的事情是這樣的:

  • 開始循環(索引開始0)
  • 讓您的第一個答案(指數目前等於1)
  • 讓你的第二個答案(指數目前等於2)
  • 獲取你的第三個答案(索引仍然等於2)
  • 輸出結果的索引(索引仍然是2,所以只有最後一個答案被處理)
  • 遞增索引,返回到循環開始(索引變成3,它終止你的循環)

循環的目的是循環遍歷數組的索引,但是每次收集答案後手動遞增索引都會導致意外行爲。簡單的解決方法是將測試您的答案的代碼分解到自己的循環中,這樣您就可以重新開始索引。如果這不能幫助你解決問題,我會將代碼添加到答案中。

+0

如果你可以添加能幫助我很多的代碼。 – Titan 2012-04-09 01:51:59

0

我相信大部分早期的海報都回答了你,並勾畫出了其他答案。我只是發佈這個,因爲我點擊了。

public class AstroQuiz { 

    final public void run() { 
     String[] questions = { 
      "What planet has the largest rings? \n\nA. Neptune \nB. Saturn \nC. Jupiter", 
      "What planet has a large redspot? \n\nA. Neptune \nB. Saturn \nC. Jupiter", 
      "What planet is farthest from the sun? \n\nA. Neptune \nB. Saturn \nC. Jupiter" 
     } 
     String rightAnswers[] = { "B", "C", "A" }; 
     String validChoices[] = {"A", "B", "C"} 

     int i = 0; 
     int correct = 0; 
     for (String question : questions) { 
      String choice = askQuestionAndGetAnswer(question, validChoices); 
      if(isCorrectAnswer(rightAnswers, i, choice)) { 
       correct++; 
       /* display "you got it!" here .. */ 
      } else { 
       /* display "wrong answer!" here ... */ 
      } 
      i++ 
     } 
     System.out.format("Total correct:%d. Total incorrect: %d.\n", correct, questions.length - correct;) 
    } 

    final private String askQuestionAndGetAnswer(String question, String[] validAnswers) { 
     String answer = null; 
     boolean haveValidResponse = false; 
     while (!haveValidResponse){ 
      answer = JOptionPane.showInputDialog(question); 
      haveValidResponse = isValidResponse(answer, validAnswers) 
     } 
     return answer; 
    } 

    final private boolean isValidResponse(String response, String[] validAnswers) { 
     for (String s : validAnswers) { 
      if(response.equals(s)) 
       return true; 
     } 
     return false; 
    } 

    final private boolean isCorrectAnswer (String[] correctionAnswers, int questionIndex, String answer) { 
     return correctionAnswers[questionIndex].equals(answer);  
    } 
} 
+1

感謝您的反饋! – Titan 2012-04-09 02:21:38