精度为1s 只接收int型
import java.util.Scanner;public class conutdown { public static void main(String[] args) { Scanner scanner =new Scanner(System.in); int time = 0; int cd = 0; int saveTime = 0; System.out.println(“请输入倒计时的秒数:”); saveTime = scanner.nextInt(); cd = saveTime; System.out.println("准备倒计时" + saveTime + "秒"); while (time < saveTime) {///循环一次1s 当循环次数大于输入次数时 跳出循环 try { Thread.currentThread().sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } time++;///自增用于与savetime进行比较。 cd--;//自减实现倒计时。 System.out.println("还剩" + cd + "秒,已消耗" + time + "秒"); } System.out.println(“计时结束”); scanner.close(); }}