Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2014
    Posts
    28
    Rep Power
    0

    Question Using a Timer in a Java Quiz

    Greetings, fellow Java programmers.
    I am currently making a quiz for a project. I am almost finished, but I need to use a timer.

    Here is what I want to happen:
    The user to has 15 seconds to answer each question. If they answer, they are given the option to move to the next question, or leave the quiz. If they answer incorrectly, the quiz closes. If they do not answer within the 15 seconds, the program treats this as an incorrect answer, and the quiz closes.


    Here is a section of the quiz code which includes the start of the code to the end of the code for the first question:
    Java Code:
    package quiz;
    
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.util.Scanner;
    
    import javax.swing.AbstractButton;
    import javax.swing.ImageIcon;
    import javax.swing.JOptionPane;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.Icon;
    
    import java.util.Timer;
    
    public class Main {
    	
    	BufferedImage img = null; 
    	BufferedImage img2 = null;
    	 
    	Image dbImage;
    	Graphics dbg;
    	 
    	private ImageIcon image;
    	private JLabel label;
    
    		public static void main(String[] args) {
    
    			int score = 0;
    			
    			int seconds = 0;
    
    			int loop1 = 0;
    			int loop2 = 0;
    			int loop3 = 0;
    			int loop4 = 0;
    			int loop5 = 0;
    			int loop6 = 0;
    			int loop7 = 0;
    			int loop8 = 0;
    			int loop9 = 0;
    			int loop10 = 0;
    			int loop11 = 0;
    			int loop13 = 0;
    			int loop14 = 0;
    			int loop15 = 0;
    			int loop16 = 0;
    			int loop17 = 0;
    			int loop18 = 0;
    			int loop19 = 0;
    			int loop20 = 0;
    			int loop21 = 0;
    			int loop22 = 0;
    
    	String name = JOptionPane.showInputDialog(null, "What is your name? ");
    	while (loop1 < 100){
    	JOptionPane.showMessageDialog(null,	"Hello " + name + ". Welcome to the Game Show. In this quiz, you will be given $1 to start with.");
    	JOptionPane.showMessageDialog(null,	"There will be 20 multiple choice questions. \n" +
    	"You will have 15 seconds to answer each question. Answer each question with the letter (A, B, C or D) corresponding with that answer.");
    	JOptionPane.showMessageDialog(null,	"If you fail to answer in that time, or if you give an incorrect answer, you will be sent home with $1. \n If you give a correct answer, your money will double.");
    	JOptionPane.showMessageDialog(null,	"After each question, you will be given the choice to either leave the game show with half of the money you have made, or continue to the next question. \n " +
    	"You will have 15 seconds to make this decision. If you do not give an answer, you will automatically proceed to the next question. \n You cannot choose to leave the game show after you proceed to the next question.");
    	int start = JOptionPane.showConfirmDialog(null, "Would you like to begin? Yes or no?");
    	if(start == JOptionPane.NO_OPTION) {
    	JOptionPane.showMessageDialog(null, "Ok. Get ready, and when you feel you are ready to begin, click 'Ok'.");
    	loop1++;
    	break;
    	}
    	if(start == JOptionPane.YES_OPTION) {
    	break;
    	}
    	else{
    	System.exit(0);
    	break;
    	}
    	}
    	while (loop2 < 1){
    	String Q1 = JOptionPane.showInputDialog(null, "Question 1: What was the original name of the Java programming language? \n A) Coffee   B) JCode   C) Oak   D) Green");
    	if(Q1.equalsIgnoreCase("C")) {
    	JOptionPane.showMessageDialog(null, "Correct!");
    	score = 2;
    	String L1 = JOptionPane.showInputDialog(null, "You now have $" + score + "! Would you like to leave the game with half of your current prize money? \n Type 'Yes' to leave, or type 'No' to continue to the next question.");
    	{
    	if(L1.equalsIgnoreCase("Yes")) {
    	score /= 2;
    	JOptionPane.showMessageDialog(null, "Congratulations. You won $" + score + "! Goodbye.");
    	System.exit(0);
    	}
    	else{
    	JOptionPane.showMessageDialog(null, "You may now proceed to the next question. Good luck!");
    	}
    	break;
    	}}
    	else{
    	JOptionPane.showMessageDialog(null, "That is not correct! \n YOU ARE OUT OF THE GAME WITH $1!");
    	System.exit(0);
    	}
    	loop2++;
    	}
    I appreciate any help you can give me :)

  2. #2
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Using a Timer in a Java Quiz

    Hi,

    Maybe you look for something like this:
    swing - Java:Show message dialog for 10sec and remove? - Stack Overflow

    With kind regards,

    Konrad

  3. #3
    Join Date
    Mar 2014
    Posts
    28
    Rep Power
    0

    Question Re: Using a Timer in a Java Quiz

    Thanks. I have been trying to implement all of that into my quiz, but it's not working. Also, I don't know whether to use java.util.Timer or javax.swing.Timer ?? Can you show me how you would do it?
    Last edited by JavaPr0grammer; 04-21-2014 at 04:49 AM.

  4. #4
    Join Date
    Mar 2014
    Posts
    28
    Rep Power
    0

    Exclamation Re: Using a Timer in a Java Quiz

    Here is something I just did:

    Java Code:
    while (loop2 < 1){
    		// start timer
    		begin = System.currentTimeMillis();
    	String Q1 = JOptionPane.showInputDialog(null, "Question 1: What was the original name of the Java programming language? \n A) Coffee   B) JCode   C) Oak   D) Green");
    	if(Q1.equalsIgnoreCase("C")) {
    		// end timer
    		end = System.currentTimeMillis();
    	JOptionPane.showMessageDialog(null, "Correct!");
    		if( end - begin < 10000 ){
    		score = 2;
    		}
    		else {
    		JOptionPane.showMessageDialog(null, "You took too long to answer the question! YOU ARE OUT OF THE GAME WITH $1!");
    		System.exit(0);
    		}
    It works! But I don't think it's the best way of solving the problem. :)

  5. #5
    Join Date
    Mar 2014
    Posts
    28
    Rep Power
    0

    Question Re: Using a Timer in a Java Quiz

    Now I'm trying to add a sound file of a countdown from 15 to 0, which I want to play when the question is asked, and stop if the use chooses an answer:

    Java Code:
    AudioClip clip = Applet.newAudioClip(file:/Users/Ethan/Desktop/Countdown.wav);
    	clip.play();
    It's not working though :(

Similar Threads

  1. I Need Help With a Java Timer for a Quiz!
    By JavaPr0grammer in forum New To Java
    Replies: 39
    Last Post: 03-11-2014, 11:52 AM
  2. HELP - Java quiz application won't run
    By MD1993 in forum New To Java
    Replies: 4
    Last Post: 04-10-2012, 04:13 PM
  3. JAVA Quiz
    By sara1988 in forum New To Java
    Replies: 4
    Last Post: 09-14-2011, 06:20 AM
  4. Swing java quiz
    By htuy in forum New To Java
    Replies: 7
    Last Post: 07-08-2011, 07:02 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •