basicallt i want to make a simple quiz...and the quiz contained 20 questions...but i have no idea in looping the question...so can anyone help me?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
class Quiz extends JFrame implements ActionListener{
private JButton submit,next;
private JTextArea text;
private JTextField answer;
private JLabel question;
private int count = 0;
private int marks = 0;
Random generator = new Random();
int number = 65 + generator.nextInt(24);
public char c =(char)number;
public Quiz() {
setSize(500,300);
setTitle("Simple Quiz");
setLocation(300,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
question = new JLabel("What comes after "+c);
answer = new JTextField(10); //need to gettext
submit = new JButton("Submit");
submit.addActionListener(this);
next = new JButton("Next Question");
next.addActionListener(this);
text = new JTextArea(10,20);
Container container = getContentPane();
container.setLayout(new FlowLayout());
container.add(question);
container.add(answer);
container.add(submit);
container.add(next);
container.add(text);
}
public void actionPerformed(ActionEvent e){
String input = answer.getText();
char letter = input.charAt(0);
if(e.getSource() == submit){
if(letter == (c+1)|| letter == (c+33)){
text.append("Correct.\n");
marks++;
}
else{
text.append("Wrong.\n");
}count++;
}
if(e.getSource() == next)
{
}
}
public static void main(String[] args){
Quiz quiz = new Quiz();
quiz.setVisible(true);
}
}