How to make a simple timer using TimeUnit.sleep

0

I am a beginner in Java and was trying to do a mathematical game whose theme was to add the random numbers of the buttons (of course the person would have the help of numbers 1, 2 and 5) until reaching a value equal to the random number generated automatically (rather than a simple calculator).

The only thing I miss, so far, is timer to give another level of difficulty to the game. However, I tried to make a simple timer to test if I could do with TimeUnit.Sleep and it did not work very well.

In this case,% w / w% w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w value of the Buttons and Jlabel again and the time would have an additional 30 seconds (but this idea is of another method that I will still do, I just quoted to complement the question)

public void GameTime() {
    int seconds = 60;
        for (int i = seconds; i < 0; i++) {
            lblGameTime.setText(String.valueOf(i));
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException ex) {
            Logger.getLogger(tela1.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

variable lblGameTime :

private int valorSomado = 0;

Method valorSomado :

private long randomNumber() {
    long ale;
    ale = (long) ((Math.random()) * (10001 - 0) + 1);
    lblRandomNumber.setText(String.valueOf(ale));
    return ale;
}

PS: The " valorRandomico " I am using is that of valorRandomico which already comes with a separate screen where you can model the entire screen on which the code will be executed

imageofsuch"code Swing /a/code" code NetBeans /code

PSS: I had the "brilliant" idea of trying not to use classes (even though I know I'm using valorSomado )

    
asked by anonymous 08.04.2016 / 20:31

0 answers