I'm doing a quiz game using netbeans.

I've already done the randomization of questions. The problem is, how do i keep a question from repeating itself?
ex:
chose correct answer --> questionrandomizer comes in --> next question appears but it's the same question earlier

here's my code:

package kiddiequiz.scienceforms;

import java.awt.Color;
import java.util.Random;
import javax.swing.JOptionPane;
import kiddiequiz.correct;



/**
 *
 * @author USER
 */
public class qsez1 extends javax.swing.JFrame {

    /**
     * Creates new form qsez1
     */
    public qsez1() {
        initComponents();
         getContentPane().setBackground(new Color(255,51,51));
        setResizable(false);



    }                                                                      

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        questionLabel = new javax.swing.JLabel();
        choice1 = new javax.swing.JButton();
        choice2 = new javax.swing.JButton();
        choice3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        questionLabel.setFont(new java.awt.Font("Comic Sans MS", 1, 36)); // NOI18N
        questionLabel.setForeground(new java.awt.Color(255, 204, 204));

        choice1.setBackground(new java.awt.Color(255, 51, 51));
        choice1.setFont(new java.awt.Font("Comic Sans MS", 1, 36)); // NOI18N
        choice1.setForeground(new java.awt.Color(255, 153, 102));
        choice1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                choice1ActionPerformed(evt);
            }
        });

        choice2.setBackground(new java.awt.Color(255, 51, 51));
        choice2.setFont(new java.awt.Font("Comic Sans MS", 1, 36)); // NOI18N
        choice2.setForeground(new java.awt.Color(255, 153, 102));
        choice2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                choice2ActionPerformed(evt);
            }
        });

        choice3.setBackground(new java.awt.Color(255, 51, 51));
        choice3.setFont(new java.awt.Font("Comic Sans MS", 1, 36)); // NOI18N
        choice3.setForeground(new java.awt.Color(255, 153, 102));
        choice3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                choice3ActionPerformed(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()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(questionLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(279, 279, 279)
                        .addComponent(choice1, javax.swing.GroupLayout.PREFERRED_SIZE, 294, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(190, 190, 190)
                        .addComponent(choice2, javax.swing.GroupLayout.PREFERRED_SIZE, 294, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 284, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(523, 523, 523)
                .addComponent(choice3, javax.swing.GroupLayout.PREFERRED_SIZE, 294, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(54, 54, 54)
                .addComponent(questionLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(choice1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(choice2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31)
                .addComponent(choice3, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(40, Short.MAX_VALUE))
        );

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

    private void choice1ActionPerformed(java.awt.event.ActionEvent evt) {                                        

            questionrandomizer();
    }                                       

    private void choice2ActionPerformed(java.awt.event.ActionEvent evt) {                                        

    }                                       

    private void choice3ActionPerformed(java.awt.event.ActionEvent evt) {                                        

    }                                       

    private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  
        questionrandomizer();
    }                                 

  public void questionrandomizer(){
       String qez[] ={"             What is an animal that eats other animals called?", //[0] ans: predator
                       "            During which stage does a caterpillar become a chrysalis?", // [1] ans: Pupa
                       "                What do you call a scientist that studies life?", // [2] ans: biologist
                       "What do you call the series of changes that many animals go through in life?", // [3] life cycle
                       "                        An animal that changes colors?"}; // [4] ans: chameleon/YANDERE

       Random random = new Random();
       int index = random.nextInt(qez.length);
       Integer.toString(index);
       //listEZ=qez[random.nextInt()];
       System.out.print(qez[index]);

       if(index==0)
        {

           questionLabel.setText(qez[0]);
           choice1.setText("Predator");//correct
           choice2.setText("Prey");
           choice3.setText("Meanie");    

        }

       if(index==1){
           questionLabel.setText(qez[1]);
           choice1.setText("Larva");
           choice2.setText("Pupa");//correct
           choice3.setText("Egg");

       }

       if(index==2){
           questionLabel.setText(qez[2]);
           choice1.setText("Biologist");//correct
           choice2.setText("Geologist");
           choice3.setText("Physicist");

       }

       if(index==3){
           questionLabel.setText(qez[3]);
           choice1.setText("Life Cycle");//correct
           choice2.setText("Food Web");
           choice3.setText("Circle of Life");

       }

       if(index==4){
           questionLabel.setText(qez[4]);
           choice1.setText("Frog");
           choice2.setText("Lizard");
           choice3.setText("Chameleon");//correct

       }



  } 
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<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(qsez1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(qsez1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(qsez1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(qsez1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new qsez1().setVisible(true);
            }
        });
    }


    // Variables declaration - do not modify                     
    private javax.swing.JButton choice1;
    private javax.swing.JButton choice2;
    private javax.swing.JButton choice3;
    private javax.swing.JLabel questionLabel;
    // End of variables declaration                   
}

Recommended Answers

All 4 Replies

just an example of what you could do:

  Create two lists: 1 filled with questions and answers
                    1 empty list
  Each time you display a question:
                    remove this from the first list
                    add it to the second list
  Each time again:  generate a random question from the first list

of course, if you do that it's no longer random...

Another approach:

  1. Shuffle your list of questions (make a copy if the original order is important)
  2. Present them in the new order

Im using this on my game quiz.. hope it will help you too:

String[] questions = {
    "What year is launched Facebook ?", // 0
    "What year is born Michael Jackson ?",// 1
    "Which are the owner's of Google ?"
};
String[] answers = {
    "2004", // 0
    "1958", // 1
    "larry page and sergey brin"
};

int questionNumber;

final void setQuestion(int nr) {
    questionNumber = nr;
    question.setText(questions[questionNumber]);
}

void chooseNewQuestion() {
    Random rand = new Random();
    int nr;
    do {
        nr = rand.nextInt(questions.length);
    } while (nr == questionNumber);
        setQuestion(nr);
    }

/*void changeQueston() {
This is for timer if you want to change the question if is not answered in certain time But you have to build the timer by your self...

    question.setText(questions[questionNumber]);
    if (TimerTime >= 3) {
        setQuestion(WIDTH);
    }
}*/

and put theese into public qsez1()

public qsez1() {
    initComponents();
    //changeQueston(); 
    getContentPane().setBackground(new Color(255,51,51));
    setQuestion(0);
    setResizable(false);
}
commented: Completely fails to address the OP's question -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.