I would like to set and display a score that varies on how fast the correct answer was made. For example..a question would be answered for only a minute..and the score would be 20points for answering in the range of 1-10 seconds, 15points for answering in the range of 11-30 seconds, 10 points for answering in the range of 31-45seconds and 5points for the remaining time till it reaches 1minute. and no score for incorrect answers.

Thanks in advance for those who could help! :D

Recommended Answers

All 6 Replies

The Date class should help. It holds not just the date but also the time in mSecs. Its default constructor initialises it to the exact time it was created. Its getTime() method returns mSecs, so you can do this:

long start = new java.util.Date().getTime();
 // do whatever
 long end = new java.util.Date().getTime();
 System.out.println("Watever took " + (end - start)  + "mSec");

If you want to interrupt the user after 1 minute and stop waiting for him to reply, then that's another problem, involving multiple Threads. Do you need/want to go there?

The Date class should help. It holds not just the date but also the time in mSecs. Its default constructor initialises it to the exact time it was created. Its getTime() method returns mSecs, so you can do this:

long start = new java.util.Date().getTime();
 // do whatever
 long end = new java.util.Date().getTime();
 System.out.println("Watever took " + (end - start)  + "mSec");

If you want to interrupt the user after 1 minute and stop waiting for him to reply, then that's another problem, involving multiple Threads. Do you need/want to go there?

Hmm. maybe i'll go for multiple threads if that is what i need..can you explain about it?

OK, lets have a look at that. In the meantime, you will still use the method I showed above to calculate the elapsed time & thus your points.
What form of user input are you using?

im using a text field..it will get the answer..and if it matches the correctanser, it will invoke the calculation for the score..but the user will be given only a minute to answer..and it will move forward to another question after a minute and so on..

Is that a Swing JTextfield? If so, you can do this quite easily.
Read up on the javax.swing.Timer class
http://download.oracle.com/javase/6/docs/api/javax/swing/Timer.html
When you ask the Q you can then start a swing timer to run for 60,000 millisecs. When the timer completes, its run method will do whatever you wanted to do when the user hasn't responded. If the user answers while the timer is still running, just stop() the timer.

thanks i'll be reading this first and ask again if there's more that i need to clarify. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.