编写程序,提示用户输入秒数,每隔一秒显示提示剩余的秒数的信息,并在时间结束时终止程序。
以下是运行示例:Enter the number of second:3
2 seconds remaining
1 seconds remaining
Stopped
http://yuncode.net/code/c_506c52ebe894063
是 要代码吗?还是思路?
要不要 用户随时可以输入?
public void main(String[] arg0){
int count = 3;
System.out.println("Enter the number of second:"+count);
while(count>1){
sleep(1000);
System.out.println(--count+"seconds remaining");
}
System.out.println("Stopped");
}
/**
* 倒计时器
*
* @param seconds 总时间,单位为秒
* @throws InterruptedException 中断异常
*/
public static void timer(int seconds) throws InterruptedException {
if (seconds <= 0) {
throw new IllegalArgumentException("illegal param");
}
System.out.println("Enter the number of second: " + seconds);
while(seconds > 1){
Thread.sleep(1000);
System.out.println(--seconds + " seconds remaining");
}
System.out.println("Stopped");
}