倒计时

package work11countDown;

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();
		
		
		
	}	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hao难懂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值