2

So I'm making a clock program on Java, and in the console, it displays all the outputs, as it repeated goes in loops. Here's my program:

import java.util.Scanner;

public class clock
{
    public static void main(String[] args)

    throws InterruptedException
    {
        Scanner Input = new Scanner(System.in);

        System.out.print("HOUR: ");

        int hrs = Input.nextInt();

        System.out.print("MIN: ");

        int min = Input.nextInt();

        System.out.print("SEC: ");

        int sec = Input.nextInt();

        while (true)
        {
        System.out.println(hrs + ":" + min + ":" + sec);

        sec = sec + 1;

        if (sec > 59)
        {
            sec = 0;

            min = min + 1;
        }
        if (min > 59)
        {
            min = 0;

            hrs = hrs + 1;
        }
        if (hrs > 12)
        {
            hrs = 1;
        }
        Thread.sleep (1000);
        }
    }
}

In the console, I get: 9:43:35 9:43:36 9:43:37 9:43:38 9:43:39 9:43:40

How can I have it such that it shows only the current time, and not the time displayed before?

Minhas Kamal
  • 20,752
  • 7
  • 62
  • 64
Rapid Readers
  • 303
  • 1
  • 3
  • 13

0 Answers0