简体   繁体   中英

My print statement wont print my variable in my while loop

I have a block of code and I'm trying to make it so it counts down from 10, and prints it after 1 second, but nothing in the while loop will work. Any suggestions? (No errors or tips are popping up in Eclipse IDE)

    public static void main(String[] args) {
        int timer = 10;
        while (timer>10) {
            System.out.println(timer);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            timer = timer - 1;

        }
        System.out.println("Blast Off!");
    }

look at your condition in while loop and a timer value:

int timer = 10;
while (timer>10) {

You set timer to 10. timer > 10 is never met.

You set timer to 10, meaning that the program inside while (timer>10) will never gets executed.

I think you want while (timer > 0) instead

您应该将计时器条件更正为以下:

while (timer>0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM