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
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