是否可以在Java控制台应用程序中制作一个在几秒钟内倒计时的计时器?所以可以说计算10秒;打印一份声明;再计数10秒并打印另一份声明等。如果是这样我怎么能这样做?
答案 0 :(得分:0)
// 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();
}
}