0

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);   	
    }
}
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by stultuske
0

you don't want to 'loop a question', otherwise, you'ld get the same question over and over.
what you want, is to loop over a series of questions.

just use the next basic logic:

String questions[] = new String[5];
// I assume you'll fill this in yourself

for ( int i = 0; i < questions.length; i++){
  System.out.println(questions[i]);
  // handle answer
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.