Hi!

I'm creating a guessing game where I ask the user to guess a number between 1-1000.
The output where the computer asks the user for a number is contained in a do loop.

The problem I'm having is that I have a high score list and in the high score list it should say how long time it took until the user guessed the correct number. It should start counting when the user enters his first guess and end when he types the correct number. I'm using start = System.currentTimeMillis(); to count the time.

So do I have to change it to a for loop or is there some other way to solve this? I really want to make it as simple as possible and not have to change my whole code cause I'm too lazy atm.

Any advice is appreciated!

Recommended Answers

All 3 Replies

Use an if statement.

boolean firstGuess = true;
long totalTimeMillis = 0;
if (firstGuess){
...
}

That should get you started

To time how long some process takes you can use an algorithm like this:
1. set a "start" variable to System.currentTimeMillis()
2. do whatever it is you want to time
3. set a "stop" variable to System.currentTimeMillis()
4. calculate an elapsed time by subtracting the start time from the stop time
The elapsed time will be a number of milliseconds that the processing in step 2 took.

Ye, I have solved how to do it now :)
Thank you very much for your responses guys.

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.