简体   繁体   中英

Java Simple For Loop Not Working

I want the following code to count backwards from 33 to 11 but I can't figure out why this does not work. I'm sure that I'm going to have a Homer Simpson "d'oh" moment when I finally learn the answer, but for now, I'd really appreciate any help.

    for(int i = 33; i <= 11; i--)
    {
        System.out.println(i);
    }

The loop will execute only as long as i <= 11 . This is not true the very first time, so the loop never executes. Instead, you want the loop to execute as long as i >= 11 -- greater than 11, not less than 11. With that small correction, your loop will be fine.

It should be:

for(int i = 33; i >= 11; i--)
{
      System.out.println(i);
}

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