有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java重启程序和来自Jbutton的随机问题

我有一个GUI,它将显示五个随机选择的问题。问题限制受按下下一个问题按钮的次数限制。当用户完成这五个问题后,我希望他们按下启动新一轮按钮,该按钮将重新启动程序
我以前从未做过这样的事情,我看过的代码我不明白它是如何工作的

这是我的代码:这里一切正常,我只需要添加重启按钮/代码

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?
}

请帮忙

提前谢谢


共 (1) 个答案

  1. # 1 楼答案

    试试这样:

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

    如果您想显示其他内容而不是第一个问题,可以添加一些其他代码来代替displayQuestionActionPerformed()