Pergunta

I'm looking for some help with a problem I've been having lately. I want to decrement from 200 down to 0, but I don't want it to be instant, but rather I want it to decrement over the course of a second. For example, at 0.5 seconds it would be at 100, 0.75 it would be a 50 and so on. If this is at all possible, I would love to hear from you guys! -Thanks so much, Brandon

Foi útil?

Solução

To do what you want, here's the answer:

int index = 200;

while(index != 0)
{
    index--;
    System.out.println("The value is: " + index);
    try {
        //200 * 5 milliseconds = 1 second
        Thread.sleep((long) 5);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

You might want to put this in a thread. Here's a page that could help you: http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html

Outras dicas

You can try with a timer. Schedule it to run at the required interval and decrement the value as desired. There are other options as well like iterating in a loop, waiting for the required interval and then decrementing.

you can involve a Java Thread,in run method make a loop and use Thread.sleep method under condition.

you can use some thing like this. find the proportion you want to delay and delay it in each iteration using thread.sleep. in each iteration the running thread(main program) will sleep for 200 mili seconds

public static void main(String[] args) throws InterruptedException {


        for(int i = 0;i<200;i++){
            Date date = new Date();
        Thread.sleep(200); // this is mili seconds
        System.out.println(date.getTime());
        }


    }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top