菜鸟的博客

纵有疾风起,人生不言弃。

导航

Java倒计时

 

所花时间(包括上课):  0.5  h左右
代码量(行):  50     左右
搏客量(篇): 1
了解到的知识点:  java倒计时的实现,用来做个小项目
备注(其他):  
static int countDownTime=100;//用来设置总时间

Timer timer=new Timer();
        TimerTask task=new TimerTask() {
            @Override
            public void run() {
                if(countDownTime!=0){
                    countDownTime--;
                }
            }
        };
        timer.schedule(task,0,1000);//放在主函数





long currentTime = System.currentTimeMillis();
//计时180秒
int settime = 180;
if ((currentTime - startTime) / 1000 > settime) {
    System.out.println("Time's up!");
    wrongCount=30-correctCount;
    break ;
} else System.out.println(settime - (currentTime - startTime) / 1000 + "s left");
 

 

posted on 2024-04-26 17:24  hhmzd233  阅读(7)  评论(0编辑  收藏  举报