-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountdownTimer.java
More file actions
36 lines (26 loc) · 885 Bytes
/
CountdownTimer.java
File metadata and controls
36 lines (26 loc) · 885 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
public class CountdownTimer {
public static void main(String[] args) {
// Countdown Timer Program
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number of seconds to count down from: ");
int response = scanner.nextInt();
Timer timer = new Timer();
TimerTask task = new TimerTask(){
int count = response;
@Override
public void run() {
System.out.println(count);
count--;
if(count<=0) {
System.out.println("Happy New Year!");
timer.cancel();
}
}
};
timer.scheduleAtFixedRate(task, 0, 1000);
scanner.close();
}
}