0

I am currently working on a quiz game and I want to be able to start a timer when the game begins and then end the timer when the game ends. Then I want to print out how long it took the player to complete the game. Is there any simple way of doing this?

EDIT: Thank you xenteros! All I had to do was remove "long" from "long difference = stopTime - startTime;" , create a variable before that line of code like this "long difference;", and initialize the "startTime" variable.

KobiF
  • 60
  • 1
  • 12

2 Answers2

1

If your user's behavior is in a single method the code should be:

long startTime = System.currentTimeMillis();
//the method
long stopTime = System.currentTimeMillis();
long difference = stopTime - startTime;
System.out.println("The task took: " + difference + " milliseconds");
System.out.println("The task took: " + difference/1000 + " seconds");

The above is the correct way to do that in Java.

For your comfort:

public static void main() {
    long startTime, stopTime;
    //some code
    startTime = System.currentTimeMillis(); //right before user's move
    //user's move
    stopTime = System.currentTimeMillis();
    long difference = stopTime - startTime;
    System.out.println("The task took: " + difference + " milliseconds");
    System.out.println("The task took: " + difference/1000 + " seconds");

}
xenteros
  • 15,586
  • 12
  • 56
  • 91
  • When I put those in it says "startTime cannot be resolved to a variable" on the third line of code – KobiF Sep 26 '16 at 12:08
  • Now it just says duplicate local variable on both. @xenteros – KobiF Sep 26 '16 at 12:12
  • @KobiF You need to explain how exactly are you using code from this answer. If variable can't be resolved then you either didn't declare it before using it, or you are trying to use it outside of its scope (like when you declare it in one method but trying to access it inside other one). – Pshemo Sep 26 '16 at 12:12
  • @Pshemo all of this code is in the main method. – KobiF Sep 26 '16 at 12:14
  • @xenteros all of this code is in the main method and I still have the same error – KobiF Sep 26 '16 at 12:17
  • @KobiF OK, but is it ins same scope/code block? Scope is not determined only by methods. When you do something like `int i=1; {int j = 2;} i++, j++` you will not be able to increment `j` because it will be out of scope of its declaration. You also can't have `int i=1; {int i = 2; i++;}` because inside that `{..}` you would have two variables named `i`, so `i++` wouldn't know which `i` you want to increment. – Pshemo Sep 26 '16 at 12:18
  • @Pshemo well I have separate blocks of code for different questions the player has to answer. – KobiF Sep 26 '16 at 12:23
  • @KobiF I am guessing that you simply copy-passed `long startTime = System.currentTimeMillis(); ...` code in many places inside your method. Notice that `long startTime` declares variable of `long` time and names it `startTime`. We need that name to be able to use this variable, but if you use `long startTime` few times in same scope then you would try declaring few variables with same name, which means compiler wouldn't know which variable it should use. So don't declare many variables with same name `int i=1` `int i=2`. Instead reuse already declared one `int i=1; i=2`. – Pshemo Sep 26 '16 at 12:37
0

At the beginning of your main method:

final long startTime = System.nanoTime();

And then, at the end of your method :

final long duration = System.nanoTime() - startTime;

and print it

System.out.println(duration);
deviantxdes
  • 469
  • 5
  • 17
  • When I put those in it says "startTime cannot be resolved to a variable" on the second line of code. – KobiF Sep 26 '16 at 12:03
  • @KobiF check the scope of your variable – deviantxdes Sep 26 '16 at 12:06
  • Using nanoTime isn't stable. @KobiF I gave you the working solution. – xenteros Sep 26 '16 at 12:07
  • 1
    @xenteros "Using nanoTime isn't stable" can you provide more information about it? I am not saying you are wrong, I would simply like to learn more about it. – Pshemo Sep 26 '16 at 12:39
  • 1
    [Documentation](https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#nanoTime()), [StackOverflow](http://stackoverflow.com/questions/351565/system-currenttimemillis-vs-system-nanotime) – xenteros Sep 26 '16 at 12:45