一個程序,提示用戶輸入秒數,然後每秒顯示一條消息,並在時間到期時終止。到目前爲止,我已經能夠做到這一點。但是,我卡在這裏。java中的計時器實現
public static void main(String[] args)
{
Scanner r = new Scanner(System.in);
int sec = r.nextInt();
while (sec > 0)
{
System.out.println("Seconds Remaining" + sec);
/**What to do here using System.currentTimeMillis()??**/
sec--;
}
}
我敢肯定他們沒有到目前爲止覆蓋。有沒有其他方法? @Reimeus – Katherine
http://docs.oracle.com/javase/tutorial/essential/concurrency/sleep.html 文檔中的示例與您的示例幾乎完全相同... – Brick
您還可以執行旋轉循環並檢查開始時間和當前時間。不是我會推薦它,但如果你沒有了解線程的話。 – John