Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java nested loop

I'm new to java, and I'm trying to practice building nested loop.

I want the following print result:

it is now 00:00:00
it is now 00:00:01
it is now 00:00:02
it is now 00:00:03
...
it is now 11:59:59

so you see the point. It's a dumb simulation of a superfast clock

The problem is it starts with:

it is now 10 : 55 : 46
it is now 10 : 55 : 47
it is now 10 : 55 : 48

...and not with 00 : 00 : 00

So far here are my codes:

public class Example {

public static void main(String[] args)

{

    int h = 0;
    while(h<=11)
    {
        int m = 0;
        while(m<=59)
        {
            for(int s=0; s<=59;s++)
            {
                System.out.println("it is now " + h + " : " + m + " : " + s );  
            }
            m++;
        }
        h++;

    }

}

Any help will be greatly appreciated! Sylvain

like image 713
Sylvain Avatar asked Apr 10 '26 07:04

Sylvain


1 Answers

The problem is it starts with: 10:55:46, not 00:00:00

No it starts at 0. The likeliest reason why you don't see the initial values is that the console you use only shows the last xxx lines and the initial ones are not visible any longer when the program terminates.

You could insert a try { Thread.sleep(500); } catch (Exception e) {} after h++; to confirm that visually.

like image 99
assylias Avatar answered Apr 12 '26 21:04

assylias



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!