2017-06-16 62 views
-1

我有一個GUI將顯示五個隨機選擇的問題。問題限制受到下一個問題按鈕被按下的次數的限制。當用戶完成五個問題時,我希望他們按下應該重新啓動程序的開始新的一輪按鈕。
我從來沒有做過這樣的事情,我看過的代碼我不明白它是如何工作的。從Jbutton重新啓動程序和隨機問題

這裏是我的代碼:一切工作在這裏,我只需要添加重啓鍵/代碼

int nextQuestionClicked;//For number of times next question is clicked 

//Method to generate random questions 
private String setQuestions(){ 
    int match = (int) Math.floor(Math.random()*cities.size()); 
    String whatCity = cities.get(match); 
    String displayCity = "Where is " + whatCity + " located?"; 
    return displayCity; 
    } 

//What happens when next question is pressed 
private void displayQuestionActionPerformed(java.awt.event.ActionEvent evt) {             
    boolean trueOrFalse; 

    submitButton.setEnabled(true); 

    displayQuestion.setEnabled(false); 

    outputTextQuestion.setText(null); 
    outputTextQuestion.setText(setQuestions()); 
    outputAnswers.setText(null);   
    inputAnswer.setText(null);  
    outputDegree.setText(null); 

    nextQuestionClicked++; 

    int buttonLimit = 4; 

    if (nextQuestionClicked <= buttonLimit) 
    { 

     int correctAnswer = -1; 

     for (int x = 0; x < cities.size(); x++) 
     { 
      if (trueOrFalse = outputTextQuestion.getText().contains(cities.get(x))) 
      { 
       correctAnswer = x; 
       break; 
      } 
     } 
     randomAnswers = new ArrayList <Integer>(); 
     Collections.addAll(randomAnswers, correctAnswer); 

     for (int x=0; x < 3; x++) 
     { 
      int r = correctAnswer; 
      while (randomAnswers.contains(r)) 
      { 
       r = ((int)(Math.random()*100))%cities.size(); 

      } 
      Collections.addAll(randomAnswers, r); 
     } 

     Collections.shuffle(randomAnswers); 
     outputAnswers.setText(null); 

     for (int r=0; r<randomAnswers.size(); r++) 
     { 
      int hint = randomAnswers.get(r); 
      outputAnswers.append(provinces.get(hint) + "\n"); 
     } 

     inputAnswer.requestFocus(); 
} 

    else{ 
     displayQuestion.setEnabled(false); 
     submitButton.setEnabled(false); 
     newRound.setEnabled(true); 
     outputTextQuestion.setText("Start New Round!!"); 
     outputDegree.setText(null); 
     } 
} 

private void newRoundActionPerformed(java.awt.event.ActionEvent evt) {           
    outputTextQuestion.setText(null); 
    displayQuestion.setEnabled(true); 
    submitButton.setEnabled(false); 
//What code do I do here to restart the program? 
} 

請幫幫忙!

在此先感謝!

+3

所以你問我們爲你寫代碼?這不是一種編碼服務... – ItamarG3

+0

*「我看過的代碼我不明白它是如何工作的」*這給了讀者一種印象,你不會理解我們提供的任何代碼。那麼爲什麼有人會試圖幫助?鏈接到你看到的例子,解釋你不明白的東西。嘗試一些東西,當你被困住並且有一個真正的問題(並且具體)時,回覆給我們。一般建議:爲了更快地獲得更好的幫助,請發佈[MCVE]或[簡短,獨立,正確的示例](http://www.sscce.org/)。 –

回答

1

嘗試這樣:

private void newRoundActionPerformed(java.awt.event.ActionEvent evt) {           
    nextQuestionClicked = 0; 
    displayQuestionActionPerformed(evt); 
} 

,如果你想顯示等,而不是第一個問題是你可能會增加而不是displayQuestionActionPerformed()一些其他的代碼...

+0

非常感謝你! –