具体题目如下:
具体代码如下:
package TimeCounter;
public class TimeCounert {
/*本题需要用到sleep()来控制停顿
* 在网上查了一下具体有两种方法,在此我用来第一种,1000毫秒也就是1秒
* (1)public static void sleep(long millis) throws InterruptedException ,mills 毫秒
* (2)public static void sleep(long millis, int nanos) throws InterruptedException ,millis 毫秒, nanos 纳秒。
*/
static void startCount(int begin) throws InterruptedException {//前面的public 可以加也可以省去
while(begin>0) {
System.out.println(begin);
Thread.sleep(1000);//1000毫秒=1秒;
begin--;
}
System.out.println("程序已结束,退出程序!");
}
public static void main(String[] args) throws InterruptedException {
startCount(7);//给其一个值;
}
}
运行结果如下: