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
}