1.0.0版本
实现功能:实现倒数功能
更新:无
import org.junit.Test;
public class time {
@Test
public void timeTest() throws Exception{
//设定计时时间
int total;
int HH = 0;
int mm = 0;
int ss = 10;
//计算总秒数
total = HH * 60 * 60 + mm * 60 + ss ;
//每过一秒循环一次,total总数减一
for (int i = 0; i < total; total--) {
System.out.println("剩余时间为:"+total+"秒");
Thread.sleep(1000);
}
//计时结束待触发事件
System.out.println("计时结束!");
}
}
输出样式:
![](https://img.haomeiwen.com/i10535524/1e051ebe4ca17970.png)
1.0.1版本
实现功能:无
更新:优化剩余时间显示
import org.junit.Test;
public class time {
@Test
public void timeTest() throws Exception{
String shh;
String smm;
String sss;
//设定计时时间
int total;
int HH = 0;
int mm = 1;
int ss = 5;
//计算总秒数
total = HH * 60 * 60 + mm * 60 + ss ;
//每过一秒循环一次,total总数减一
for (int i = 0; i < total; total--) {
//如果剩余秒数为0,如果mm>0,怎会将ss重置为59秒,mm减一
if(ss > 0){
ss--;
}else{
if(mm > 0){
mm--;
ss = 59;
}else if(HH>0){
HH--;
mm=59;
}
}
//格式化输出为2位
if(HH<10){ shh = "0"+HH;}else {shh = String.valueOf(HH);}
if(mm<10){ smm = "0"+mm;}else {smm = String.valueOf(mm);}
if(ss<10){ sss = "0"+ss;}else {sss = String.valueOf(ss);}
//输出剩余时间
System.out.println(shh+":"+smm+":"+sss);
//间隔1s执行
Thread.sleep(1000);
}
//计时结束待触发事件
System.out.println("计时结束!");
}
}
输出样式:
![](https://img.haomeiwen.com/i10535524/6cf66432299a095a.png)
网友评论