java60秒倒计时

package cn.xxs;

import java.util.Timer;
import java.util.TimerTask;
/**
 * java演示倒计时
 * xxs
 */
public class CountDown {	
	   
	    public static long midTime;
	    public static void main(String[] args) {	       
	        midTime = 60;

	        // time1();//方式一
	        time2();// 方式二
	    }

	    /**
	     * 方式1
	     */
	    private static void time1() {
	        Timer timer = new Timer();
	        timer.schedule(new TimerTask() {
	            public void run() {
	                midTime--;
	                long ss = midTime % 60;
	                System.out.println("还剩" +  ss + "秒");
	            }
	        }, 0, 1000);
	    }

	    /**
	     * 方式2
	     */
	    private static void time2() {

	        while (midTime > 0) {
	            midTime--;	           
	            long ss = midTime % 60;
	            System.out.println("还剩" + ss + "秒");
	            try {
	                Thread.sleep(1000);
	            } catch (InterruptedException e) {
	                e.printStackTrace();
	            }
	        }
	    }	   
}

结果:

发布了141 篇原创文章 · 获赞 33 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/qq_43560721/article/details/103699321