Page 2 of 2 FirstFirst 12
Results 21 to 40 of 40
Like Tree2Likes

Thread: I Need Help With a Java Timer for a Quiz!

  1. #21
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default Re: I Need Help With a Java Timer for a Quiz!

    Quote Originally Posted by JavaPr0grammer View Post
    Nope. It still says: 'Begin cannot be resolved to a variable' and 'end cannot be resolved to a variable.'
    A 'scope' is an area in your text where a variable can be 'seen' by your compiler; the variable is 'visible' or defined in that area; those areas are always a block between curly brackets { ... } outside of those brackets a variable defined inside those brackets is not defined or not 'visible'. Note that (almost) everything in Java is between curly brackets: classes, method bodies, for-loop bodies etc. Look carefully at your code and place the 'start' and 'end' variables between suitable curly braces:

    Java Code:
    {
       long start= System.currentTimeMillis();
       // quiz code goes here ...
       long end= System.currentTimeMillis();
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  2. #22
    Join Date
    Mar 2014
    Posts
    26
    Rep Power
    0

    Default Re: I Need Help With a Java Timer for a Quiz!

    What should I change to fix this?

  3. #23
    Join Date
    Mar 2014
    Posts
    26
    Rep Power
    0

    Default Re: I Need Help With a Java Timer for a Quiz!

    Thanks! I think our messages crossed over again. I didn't see yours before I wrote my latest one. One question, before I edit my code, is that I want to have some code before and after the two 'current times'. I only want the time to be recorded right before the first question; there is a window that pops up first, asking for the user's name, and a second one asking if the user is ready to begin. After the second 'current time', I want to have a window that shows the score of the user and the time taken (begin - end). Can I still do this, or will I have to do it another way? Take a look at the code in my other posts on this thread (that is how I had it before your message).
    Last edited by JavaPr0grammer; 03-09-2014 at 08:21 AM.

  4. #24
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default Re: I Need Help With a Java Timer for a Quiz!

    Quote Originally Posted by JavaPr0grammer View Post
    What should I change to fix this?
    As I sketched in my previous reply; you can even make those variables member variables of the class object so they will be always in scope.

    edit: and my reply crossed yours again *sigh* ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #25
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: I Need Help With a Java Timer for a Quiz!

    Look at the other variable that is used in the JOptionPane message. Where is it defined? Move the definitions for begin and end next to the defintion for name.

    Leave the code that assigns values to begin and end where they are.
    If you don't understand my response, don't ignore it, ask a question.

  6. #26
    Join Date
    Mar 2014
    Posts
    26
    Rep Power
    0

    Default Re: I Need Help With a Java Timer for a Quiz!

    When I try to define the variable on a new line, directly underneath the declaration of the name variable (see line 46 of code in previous posts)
    long begin;
    It comes up with an error:
    I Need Help With a Java Timer for a Quiz!-screen-shot-2014-03-10-8.50.30-am.png
    What do I do?????
    Please just copy the code that I uploaded from previous posts and show me how you would do it.
    Thanks :)

  7. #27
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: I Need Help With a Java Timer for a Quiz!

    what happened when you did what I suggested above:
    Move the definitions for begin and end next to the defintion for name.

    Assign begin and end values where they are currently defined.

    I don't understand what the message in the image means.
    If you don't understand my response, don't ignore it, ask a question.

  8. #28
    Join Date
    Mar 2014
    Posts
    26
    Rep Power
    0

    Default Re: I Need Help With a Java Timer for a Quiz!

    Thanks for the help guys :) I fixed the problem. I really appreciate all the help you've given me :)

  9. #29
    Join Date
    Mar 2014
    Posts
    26
    Rep Power
    0

    Default Re: I Need Help With a Java Timer for a Quiz!

    WAIT!!! I'm having more problems. It was all going well until tonight, when after a brief test of the program, I discovered that I was getting negative numbers in the billions as my times (end - begin). What can I do to fix this?
    Attached Thumbnails Attached Thumbnails I Need Help With a Java Timer for a Quiz!-screen-shot-2014-03-10-9.20.42-pm.png  

  10. #30
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default Re: I Need Help With a Java Timer for a Quiz!

    The variable 'end' is supposed to be later in time than 'start', i.e. start+delta == end where 'delta' >= 0. If 'delta' < 0, it must've taken you eons to complete that quiz or you have a bug in your code or your hardware clock is broken ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #31
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,002
    Rep Power
    6

    Default Re: I Need Help With a Java Timer for a Quiz!

    1. find out why the number is negative
    1b. ... which means tracking down the bit of code where it turns negative
    2. reason: what is wrong with that bit of code?
    3. fix

    There is no problem here, just a wrong result. That is as normal as breathing air for programmers. It becomes a problem when you can't find a solution; a bigger problem is if you don't even try to find one.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  12. #32
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default Re: I Need Help With a Java Timer for a Quiz!

    If you calculate end-start and it ends up as a negative number, while my time is 1394451921418, we can safely guess that you forgot to set the 'end' variable.

    kind regards,

    Holmes
    SurfMan likes this.
    cenosillicaphobia: the fear for an empty beer glass

  13. #33
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: I Need Help With a Java Timer for a Quiz!

    Also why ask the user to divide by 1000 when Computers do that so easily?
    If you don't understand my response, don't ignore it, ask a question.

  14. #34
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    946
    Rep Power
    2

    Default Re: I Need Help With a Java Timer for a Quiz!

    Could it be possible that you have put some long value into an int variable? The next code example returns a negative value for me:
    Java Code:
    public class IntTest {
        public static void main(String[] args) {
            long start = System.currentTimeMillis();
            int blah = (int) start;
            System.out.println("start = " + start);
            System.out.println("blah = " + blah);
        }
    }
    start = 1394454253777
    blah = -1410117423


    Edit: JosAH is right in reply #32. That will teach me to carefully read the replies. You win this one Jos!
    Last edited by SurfMan; 03-10-2014 at 01:27 PM.
    gimbal2 likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  15. #35
    Join Date
    Mar 2014
    Posts
    26
    Rep Power
    0

    Default Re: I Need Help With a Java Timer for a Quiz!

    Norm, I did let the computer divide by 1000, but instead of giving me decimal places, it just rounded to the nearest second. Thanks for the help from everyone else. I'll see if I can fix this bug, and I'll get back to you guys if I have any questions. Thanks for all the help so far though :) I really appreciate it

  16. #36
    Join Date
    Mar 2014
    Posts
    26
    Rep Power
    0

    Default Re: I Need Help With a Java Timer for a Quiz!

    And now it works again! It's very strange. Sometimes it works, sometimes it doesnt!
    Java Code:
    package test;
    
    import javax.swing.JOptionPane;
    
    public class Main {
    
    	public static void main(String [] args) {
    		
    		int score = 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 Ran1 = (int)(Math.random() * (20-1));
    		int Ran2 = (int)(Math.random() * (20-1));
    		int Ran3 = (int)(Math.random() * (20-1));
    		int Ran4 = (int)(Math.random() * (20-1));
    		int Ran5 = (int)(Math.random() * (20-1));
    		int Ran6 = (int)(Math.random() * (20-1));
    		int Ran7 = (int)(Math.random() * (20-1));
    		int Ran8 = (int)(Math.random() * (20-1));
    		int Ran9 = (int)(Math.random() * (20-1));
    		int Ran10 = (int)(Math.random() * (20-1));
    		int Ran11 = (int)(Math.random() * (20-1));
    		int Ran12 = (int)(Math.random() * (20-1));
    		int Ran13 = (int)(Math.random() * (20-1));
    		int Ran14 = (int)(Math.random() * (20-1));
    		int Ran15 = (int)(Math.random() * (20-1));
    		int Ran16 = (int)(Math.random() * (20-1));
    		int Ran17 = (int)(Math.random() * (20-1));
    		int Ran18 = (int)(Math.random() * (20-1));
    		int Ran19 = (int)(Math.random() * (20-1));
    		int Ran20 = (int)(Math.random() * (20-1));
    		
    	String name = JOptionPane.showInputDialog(null, "What is your name? ");
    	// define variables here
    	long begin = 0;
    	long end = 0;
    		while (loop1 < 100){
    			int start = JOptionPane.showConfirmDialog(null, "Hello " + name + ". Welcome to the Maths Quiz. Click YES when you are ready to begin.");
    			if(start == JOptionPane. YES_OPTION) {
    		break;
    	}
    	else{
    		System.exit(0);
    		break;
    	}
    	}
    		while (loop2 == 0){
    			// start timer here
    			begin = System.currentTimeMillis();
    			String Q1 = JOptionPane.showInputDialog(null, "Question 1: " + Ran1 + " x " + Ran2);
    			if(Q1.equals("" + (Ran1 * Ran2) + "")) {
    			score += 1;
    			break;
    			}
    			else{
    			JOptionPane.showMessageDialog(null, "That is not correct! The answer was " + Ran1 * Ran2);
    			}
    			loop2++;
    			}
    			
    	while (loop3 == 0){
    		String Q2 = JOptionPane.showInputDialog(null, "Question 2: " + Ran3 + " x " + Ran4);
    		if(Q2.equals("" + (Ran3 * Ran4) + "")) {
    		score += 1;
    		break;
    		}
    		else{
    		JOptionPane.showMessageDialog(null, "That is not correct! The answer was " + Ran3 * Ran4);
    		}
    		loop3++;
    		}
    	
    	while (loop4 == 0){
    		String Q3 = JOptionPane.showInputDialog(null, "Question 3: " + Ran5 + " x " + Ran6);
    		if(Q3.equals("" + (Ran5 * Ran6) + "")) {
    		score += 1;
    		break;
    		}
    		else{
    		JOptionPane.showMessageDialog(null, "That is not correct! The answer was " + Ran5 * Ran6);
    		}
    		loop4++;
    		}
    	
    	while (loop5 == 0){
    		String Q4 = JOptionPane.showInputDialog(null, "Question 4: " + Ran7 + " x " + Ran8);
    		if(Q4.equals("" + (Ran7 * Ran8) + "")) {
    		score += 1;
    		break;
    		}
    		else{
    		JOptionPane.showMessageDialog(null, "That is not correct! The answer was " + Ran7 * Ran8);
    		}
    		loop5++;
    		}
    	
    	while (loop6 == 0){
    		String Q5 = JOptionPane.showInputDialog(null, "Question 5: " + Ran9 + " x " + Ran10);
    		if(Q5.equals("" + (Ran9 * Ran10) + "")) {
    		score += 1;
    		break;
    		}
    		else{
    		JOptionPane.showMessageDialog(null, "That is not correct! The answer was " + Ran9 * Ran10);
    		}
    		loop6++;
    		}
    	
    	while (loop7 == 0){
    		String Q6 = JOptionPane.showInputDialog(null, "Question 6: " + Ran11 + " x " + Ran12);
    		if(Q6.equals("" + (Ran11 * Ran12) + "")) {
    		score += 1;
    		break;
    		}
    		else{
    		JOptionPane.showMessageDialog(null, "That is not correct! The answer was " + Ran11 * Ran12);
    		}
    		loop7++;
    		}
    	
    	while (loop8 == 0){
    		String Q7 = JOptionPane.showInputDialog(null, "Question 7: " + Ran13 + " x " + Ran14);
    		if(Q7.equals("" + (Ran13 * Ran14) + "")) {
    		score += 1;
    		break;
    		}
    		else{
    		JOptionPane.showMessageDialog(null, "That is not correct! The answer was " + Ran13 * Ran14);
    		}
    		loop8++;
    		}
    	
    	while (loop9 == 0){
    		String Q8 = JOptionPane.showInputDialog(null, "Question 8: " + Ran15 + " x " + Ran16);
    		if(Q8.equals("" + (Ran15 * Ran16) + "")) {
    		score += 1;
    		break;
    		}
    		else{
    		JOptionPane.showMessageDialog(null, "That is not correct! The answer was " + Ran15 * Ran16);
    		}
    		loop9++;
    		}
    	
    	while (loop10 == 0){
    		String Q9 = JOptionPane.showInputDialog(null, "Question 9: " + Ran17 + " x " + Ran18);
    		if(Q9.equals("" + (Ran17 * Ran18) + "")) {
    		score += 1;
    		break;
    		}
    		else{
    		JOptionPane.showMessageDialog(null, "That is not correct! The answer was " + Ran17 * Ran18);
    		}
    		loop10++;
    		}
    	
    	while (loop11 == 0){
    		String Q10 = JOptionPane.showInputDialog(null, "Question 10: " + Ran19 + " x " + Ran20);
    		if(Q10.equals("" + (Ran19 * Ran20) + "")) {
    		score += 1;
    		break;
    		}
    		else{
    		JOptionPane.showMessageDialog(null, "That is not correct! The answer was " + Ran19 * Ran20);
    		}
    		// stop timer here
    		end = System.currentTimeMillis();
    		loop11++;
    		}
    		JOptionPane.showMessageDialog(null, "Congratulations " + name + "! You answered " + score + " questions correctly in " + (end - begin) + " milliseconds! Divide your time by 1000 to get the seconds.");
    		}
    }

  17. #37
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default Re: I Need Help With a Java Timer for a Quiz!

    Move line #177 just before line #180; this all happened because your indentation stinks; if you're using Eclipse, a simple Ctrl-A Ctrl-Shift-F can solve this.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  18. #38
    Join Date
    Mar 2014
    Posts
    26
    Rep Power
    0

    Default Re: I Need Help With a Java Timer for a Quiz!

    Thanks Jos, I did what you said and it is working. I'll test it at different times throughout the next day, to see if there is still bugs with the timer. Thanks :)

  19. #39
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    946
    Rep Power
    2

    Default Re: I Need Help With a Java Timer for a Quiz!

    Quote Originally Posted by JavaPr0grammer View Post
    bugs with the timer. Thanks :)
    That is probably a beautiful thing called integer division. If you divide by an integer, the result is an integer. So, you have to divide by a double. How? Add a "d" to 1000 so it reads 1000d. Weird, but that's how it works. Try my example below, you'll see what happens....

    Java Code:
    import java.util.Random;
    
    public class IntDivTest {
        public static void main(String[] args) {
            long a = System.currentTimeMillis();
            try {
                //wait random between 1 and 3 seconds
                Thread.sleep(1000 + new Random().nextInt(2000));
            }
            catch (InterruptedException e) {
                //ignore
            }
            long b = System.currentTimeMillis();
    
            System.out.println("Millis = " + (b - a));
            System.out.println("/1000 = " + (b-a)/1000);
            System.out.println("/1000d = " + (b-a)/1000d);
    
        }
    }
    Possible output:
    Java Code:
    Millis = 2955
    /1000 = 2
    /1000d = 2.955
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  20. #40
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default Re: I Need Help With a Java Timer for a Quiz!

    Or use 1000.0 for a more 'natural look'.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Page 2 of 2 FirstFirst 12

Similar Threads

  1. HELP - Java quiz application won't run
    By MD1993 in forum New To Java
    Replies: 4
    Last Post: 04-10-2012, 03:13 PM
  2. Please help with java quiz (should be easy to help)
    By karoloydi in forum New To Java
    Replies: 1
    Last Post: 11-29-2011, 02:59 AM
  3. JAVA Quiz
    By sara1988 in forum New To Java
    Replies: 4
    Last Post: 09-14-2011, 05:20 AM
  4. Swing java quiz
    By htuy in forum New To Java
    Replies: 7
    Last Post: 07-08-2011, 06: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
  •