Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 即使单击“否”按钮,程序也始终重复_Java_Swing_Joptionpane - Fatal编程技术网

Java 即使单击“否”按钮,程序也始终重复

Java 即使单击“否”按钮,程序也始终重复,java,swing,joptionpane,Java,Swing,Joptionpane,在回答完问题后,JFrame中会出现一个考官,弹出一个JOptionPane询问您是否想重考,我单击“否”,程序仍在运行 package quizmaker; import javax.swing.JOptionPane; public class QuizMaker extends javax.swing.JFrame { private static QuestionBuilder Questions = new QuestionBuilder(); private s

在回答完问题后,
JFrame
中会出现一个考官,弹出一个
JOptionPane
询问您是否想重考,我单击“否”,程序仍在运行

package quizmaker;

import javax.swing.JOptionPane;

public class QuizMaker extends javax.swing.JFrame {

    private static QuestionBuilder Questions = new QuestionBuilder();
    private static String[] Question = new String[5];
    private static String[] Answer = new String[5];
    private static String[] Bogus1 = new String[5];
    private static String[] Bogus2 = new String[5];
    private static String[] Bogus3 = new String[5];
    private static int[] QuestionsIndex = {0, 1, 2, 3, 4};
    private static int Index=0, Score=0, QuestionNumber=0;

    public QuizMaker() {
        initComponents();
        goToNext();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setResizable(false);

        jLabel1.setText("jLabel1");

        jLabel2.setText("jLabel2");

        jTextField1.setEditable(false);
        jTextField1.setText("jTextField1");

        jButton1.setText("jButton1");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        jButton2.setText("jButton2");
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });

        jButton3.setText("jButton3");
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton3MouseClicked(evt);
            }
        });

        jButton4.setText("jButton4");
        jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton4MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel2))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jButton3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE)
                            .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE)
                            .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(jButton4))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
        // TODO add your handling code here:
        String YourAnswer = jButton1.getText();
        checkAnswer(YourAnswer);
        goToNext();
    }                                     

    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {                                      
        // TODO add your handling code here:
        String YourAnswer = jButton2.getText();
        checkAnswer(YourAnswer);
        goToNext();
    }                                     

    private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {                                      
        // TODO add your handling code here:
        String YourAnswer = jButton3.getText();
        checkAnswer(YourAnswer);
        goToNext();
    }                                     

    private void jButton4MouseClicked(java.awt.event.MouseEvent evt) {                                      
        // TODO add your handling code here:
        String YourAnswer = jButton4.getText();
        checkAnswer(YourAnswer);
        goToNext();
    }                                     

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(QuizMaker.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(QuizMaker.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(QuizMaker.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(QuizMaker.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        for (int x=0; x<=4; x++) {
            QuestionNumber = x + 1;
            Question[x] = JOptionPane.showInputDialog(null, "Enter Question # " + QuestionNumber, "Question # " + QuestionNumber);
            Answer[x] = JOptionPane.showInputDialog(null, "Enter Answer in Question # " + QuestionNumber, "Answer in Question # " + QuestionNumber);
            Bogus1[x] = JOptionPane.showInputDialog(null, "Enter Bogus 1 in Question # " + QuestionNumber, "Bogus 1 in Question # " + QuestionNumber);
            Bogus2[x] = JOptionPane.showInputDialog(null, "Enter Bogus 2 in Question # " + QuestionNumber, "Bogus 2 in Question # " + QuestionNumber);
            Bogus3[x] = JOptionPane.showInputDialog(null, "Enter Bogus 3 in Question # " + QuestionNumber, "Bogus 3 in Question # " + QuestionNumber);
        }
        QuestionNumber=1;
        for (int x=4; x>=0; x--) {
            int y=x+1;
            int w=(int)(Math.random()*y);
            int Container = QuestionsIndex[w];
            QuestionsIndex[w] = QuestionsIndex[x];
            QuestionsIndex[x] = Container;
        }
        Message("Let's start the quiz.");
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new QuizMaker().setVisible(true);
            }
        });
    }
    public static void goToNext() {
        if (QuestionNumber > 5) {
            Message("Your score is " + Score + " out of 5.");
            int ConfirmButton = JOptionPane.YES_NO_CANCEL_OPTION;
            JOptionPane.showConfirmDialog(null, "Do yo want to retake?", null, ConfirmButton);
            if (ConfirmButton == JOptionPane.YES_OPTION) {
                Score=0;
                QuestionNumber=1;
                for (int x=4; x>=0; x--) {
                    int y=x+1;
                    int w=(int)(Math.random()*y);
                    int Container = QuestionsIndex[w];
                    QuestionsIndex[w] = QuestionsIndex[x];
                    QuestionsIndex[x] = Container;
                }
                goToNext();
            }
            else {
                System.exit(0);
            }
        }
        else if (QuestionNumber <= 5) {
            jLabel1.setText("Question " + QuestionNumber + " of 5");
            jLabel2.setText("Score: " + Score);
            Questions.setQuestion(Question[QuestionsIndex[QuestionNumber-1]]);
            Questions.setAnswer(Answer[QuestionsIndex[QuestionNumber-1]]);
            Questions.setBogus1(Bogus1[QuestionsIndex[QuestionNumber-1]]);
            Questions.setBogus2(Bogus2[QuestionsIndex[QuestionNumber-1]]);
            Questions.setBogus3(Bogus3[QuestionsIndex[QuestionNumber-1]]);
            String[] Choices = new String[4];
            Choices[0] = Questions.getAnswer();
            Choices[1] = Questions.getBogus1();
            Choices[2] = Questions.getBogus2();
            Choices[3] = Questions.getBogus3();
            for (int x=3; x>=0; x--) {
                int y=x+1;
                int w=(int)(Math.random()*y);
                String Container = Choices[w];
                Choices[w] = Choices[x];
                Choices[x] = Container;
            }
            jTextField1.setText(Questions.getQuestion());
            jButton1.setText(Choices[0]);
            jButton2.setText(Choices[1]);
            jButton3.setText(Choices[2]);
            jButton4.setText(Choices[3]);
            QuestionNumber++;
        }
    }
    public static void checkAnswer(String YourAnswer) {
        if (YourAnswer.equals(Questions.getAnswer())) {
            Message("Correct!");
            Score++;
        }
        else {
            Message("Wrong!");
        }
    }
    public static void Message(Object Message) {
        JOptionPane.showMessageDialog(null, Message);
    }

    // Variables declaration - do not modify                     
    private static javax.swing.JButton jButton1;
    private static javax.swing.JButton jButton2;
    private static javax.swing.JButton jButton3;
    private static javax.swing.JButton jButton4;
    private static javax.swing.JLabel jLabel1;
    private static javax.swing.JLabel jLabel2;
    private static javax.swing.JTextField jTextField1;
    // End of variables declaration                   
}
packagequizmaker;
导入javax.swing.JOptionPane;
公共类QuizMaker扩展了javax.swing.JFrame{
私有静态QuestionBuilder Questions=新建QuestionBuilder();
私有静态字符串[]问题=新字符串[5];
私有静态字符串[]应答=新字符串[5];
私有静态字符串[]Bogus1=新字符串[5];
私有静态字符串[]Bogus2=新字符串[5];
私有静态字符串[]Bogus3=新字符串[5];
私有静态int[]问题索引={0,1,2,3,4};
私有静态整数指数=0,分数=0,问题编号=0;
公共QuizMaker(){
初始化组件();
goToNext();
}
@抑制警告(“未选中”)
//                           
私有组件(){
jLabel1=newjavax.swing.JLabel();
jLabel2=newjavax.swing.JLabel();
jTextField1=newjavax.swing.JTextField();
jButton1=newjavax.swing.JButton();
jButton2=newjavax.swing.JButton();
jButton3=newjavax.swing.JButton();
jButton4=newjavax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setCursor(新java.awt.Cursor(java.awt.Cursor.DEFAULT_Cursor));
可设置大小(假);
jLabel1.setText(“jLabel1”);
jLabel2.setText(“jLabel2”);
jTextField1.setEditable(false);
jTextField1.setText(“jTextField1”);
setText(“jButton1”);
jButton1.addMouseListener(新java.awt.event.MouseAdapter(){
public void mouseClicked(java.awt.event.MouseEvent evt){
jButton1MouseClicked(evt);
}
});
setText(“jButton2”);
jButton2.addMouseListener(新java.awt.event.MouseAdapter(){
public void mouseClicked(java.awt.event.MouseEvent evt){
jButton2MouseClicked(evt);
}
});
setText(“jButton3”);
addMouseListener(新java.awt.event.MouseAdapter(){
public void mouseClicked(java.awt.event.MouseEvent evt){
jButton3MouseClicked(evt);
}
});
setText(“jButton4”);
jButton4.addMouseListener(新java.awt.event.MouseAdapter(){
public void mouseClicked(java.awt.event.MouseEvent evt){
jButton4MouseClicked(evt);
}
});
javax.swing.GroupLayout=newjavax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(布局);
layout.setHorizontalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextField1)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,javax.swing.GroupLayout.DEFAULT\u SIZE,Short.MAX\u值)
.addComponent(jLabel2))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.training,false)
.addComponent(jButton3,javax.swing.GroupLayout.Alignment.LEADING,javax.swing.GroupLayout.DEFAULT_SIZE,190,Short.MAX_值)
.addComponent(jButton1,javax.swing.GroupLayout.Alignment.LEADING,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,false)
.addComponent(jButton2,javax.swing.GroupLayout.DEFAULT_SIZE,190,Short.MAX_值)
.addComponent(jButton4,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField1,javax.swing.GroupLayout.PREFERRED\u SIZE,javax.swing.GroupLayout.DEFAULT\u SIZE,javax.swing.GroupLayout.PREFERRED\u SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton3)
.addComponent(jButton4))
.addContainerGap(javax.swing.GroupLayout.DEFAULT\u SIZE,Short.MAX\u VALUE))
);
包装();
}//                         
私有void jButton1MouseClicked(java.awt.event.MouseEvent evt){
//TODO在此处添加您的处理代码:
把答案串起来=
int selected = JOptionPane.showConfirmDialog(null, "Do yo want to retake?", null, JOptionPane.YES_NO_CANCEL_OPTION);
if(selected == JOptionPane.YES_OPTION){
   ...
}