Sylvain
Sylvain

Reputation: 563

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

Upvotes: 3

Views: 695

Answers (1)

assylias
assylias

Reputation: 328568

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.

Upvotes: 8

Related Questions