62,615
社区成员
发帖
与我相关
我的任务
分享
public class 倒计时时钟 {
//小时
private int hours;
//分钟
private int min;
//秒
private int second;
public 倒计时时钟(int hours,int min,int second) {
this.hours = hours;
this.min = min;
this.second = second;
}
public int getHours() {
return hours;
}
public void setHours(int hours) {
this.hours = hours;
}
public int getMin() {
return min;
}
public void setMin(int min) {
this.min = min;
}
public int getSecond() {
return second;
}
public void setSecond(int second) {
this.second = second;
}
}
public class 倒计时 implements Runnable{
private 倒计时时钟 clock;
private long time;
public 倒计时(倒计时时钟 clock) {
this.clock = clock;
//将时间换算成秒
time = clock.getHours()*60*60+clock.getMin()*60+clock.getSecond();
}
public void run() {
while(time >= 0) {
try {
Thread.sleep(1000);
time -= 1;//时间减去一秒
clock.setHours((int)time/(60*60));
clock.setMin((int)(time/60)%60);
clock.setSecond((int)time % 60);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//return clock;
}
public 倒计时时钟 getTime() {
return clock;
}
public static void main(String [] args) {
倒计时时钟 clock = new 倒计时时钟(0,10,0);
倒计时 jishi = new 倒计时(clock);
显示 show = new 显示(jishi.getTime());
//显示 show = new 显示();
new Thread(show).start();
new Thread(jishi).start();
}
}
class 显示 implements Runnable {
private 倒计时时钟 clock;
public 显示(倒计时时钟 clock) {
this.clock = clock;
}
public void run() {
while(clock.getHours() != 0 ||
clock.getMin() != 0 ||
clock.getSecond() != 0) {
try {
System.out.println(String.format("%02d",clock.getHours())+
":"+String.format("%02d",clock.getMin())+
":"+String.format("%02d",clock.getSecond()));
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}