【发布时间】:2014-05-17 14:36:27
【问题描述】:
是否可以在 Java 控制台应用程序中制作一个以秒为单位倒计时的计时器?那么可以说数10秒吗?打印一份声明;再数 10 秒并打印另一条语句等。如果是这样,我该怎么做?
【问题讨论】:
是否可以在 Java 控制台应用程序中制作一个以秒为单位倒计时的计时器?那么可以说数10秒吗?打印一份声明;再数 10 秒并打印另一条语句等。如果是这样,我该怎么做?
【问题讨论】:
// This is how to print with a delay
// Thread.sleep stops the program executoin for an amount of time
System.out.println("String 1");
try {
Thread.sleep(1000); // Thread.sleep takes in milliseconds
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("String 2");
// This is how to count down with a delay. Its accuracy is questionable, because
// printing and increminting k take time, but it should be very close to a second.
for(int k = 10; k > 0; k--)
{
System.out.println(k);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
【讨论】: