I'm writing a simple java program to ask random math questions. I'm just using the console so nothing fancy.
How can I time how long it takes the user to answer a question from when it is first asked to when they key in an answer and hit return?
I'm writing a simple java program to ask random math questions. I'm just using the console so nothing fancy.
How can I time how long it takes the user to answer a question from when it is first asked to when they key in an answer and hit return?
Call System.currentTimeMillis()
to get the time in milliseconds before and after each question.
Use a date to save the time when the question was displayed for the first time.
import java.util.Date;
Date startDate = new Date();
Then after the answer was submitted, subtract the date from the current time
long totalMilliseconds = (new Date()).getTime() - startDate.getTime();
Simply save the date when the question is shown
Date start = new Date();
Do the same when an answer is received and then calculate the difference and parse it to seconds or minutes:
long diff = start.getTime() - end.getTime();
long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;