import java.util.concurrent.TimeUnit;
public class Decrease {
public Decrease(int hour, int minute, int second) {
showTime(hour, minute, second);
while(true) {
second--;
if(second < 0) {
minute -= 1;
if(minute < 0) {
hour -= 1;
minute = 59;
}
second = 59;
}
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(second == 0 && minute == 0 && hour == 0) {
showTime(hour, minute, second);
System.out.println("本次倒计时结束,感谢使用");
System.exit(0);
}else {
showTime(hour, minute, second);
}
}
}
public void showTime(int hour, int minute, int second) {
System.out.printf("倒计时为:%3d:%3d:%3d \n", hour, minute, second);
}
}
package work11countDown;
public class Display {
private int hour;
private int minute;
private int second;
private boolean flag; //默认值为false
public Display(int hour, int minute, int second){
if(hour>=0){
this.hour = hour;
flag = true;
}else{
System.out.println("请输入非负数");
return;
}
if(minute>=0){
this.minute = minute;
flag = true;
}else{
System.out.println("请输入非负数");
return;
}
if(second>=0){
this.second = second;
flag = true;
}else{
System.out.println("请输入非负数");
flag = false; //最后需要将flag改为false,否则它通过"时"、"分"是true,直接return就还是true
return;
}
}
public void showStartTime(){
if(flag == true){
System.out.println("倒计时即将启动....");
new Decrease(hour, minute, second);
}
}
}
package work11countDown;
import java.util.Scanner;
public class TimeTest {
public static void main(String[] args){
System.out.println("请输入倒计时的时分秒(用空格隔开):");
Scanner sc = new Scanner(System.in);
int hour = sc.nextInt();
int minute = sc.nextInt();
int second = sc.nextInt();
sc.close();
Display display = new Display(hour, minute, second);
// Display display = new Display(0 , 0, 5);
display.showStartTime();
}
}
package work11countDown;import java.util.concurrent.TimeUnit;public class Decrease { public Decrease(int hour, int minute, int second) { showTime(hour, minute, second); while(true) { secon...