Simple Clock Countdown Simulation in Java

Problem:

Write a program that prompts the user to enter the number of seconds, displays a message at every second, and terminates when the time expires.


Output:

Enter the number of seconds: 10
10 seconds remaining
9 seconds remaining
8 seconds remaining
7 seconds remaining
6 seconds remaining
5 seconds remaining
4 seconds remaining
3 seconds remaining
2 seconds remaining
1 second remaining


Solution:

public class CountdownClock
{
  public static void main(String[] args)
  {
    java.util.Scanner scan = new java.util.Scanner(System.in);
    System.out.print("Enter the number of seconds: ");
    int seconds = scan.nextInt();
    for (int i =seconds;i>0;i--)
    {
      if (i != 1)
        System.out.println(i + " seconds remaining");
      else
        System.out.println(i + " second remaining");

    }
    
  }
}


No comments :

Post a Comment

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact