我有一个控制电子元件的程序。我的问题是倒计时部分。实际上,如果我从 main 方法调用类 CountDown,它不会返回到 main。该程序必须始终处于活动状态,它会读取 main 中的第一个值以进行调用并开始倒计时。这是代码:
public class CountDown
{
public static int a;
public static Timer timer;
public CountDown()
{
timer = new Timer();
timer.schedule(new DisplayCountdown(), 0, 1000);
}
class DisplayCountdown extends TimerTask
{
int seconds = 15;
public void run()
{
if (seconds > 0)
{
System.out.println(seconds + " seconds remaining");
if(READING BY ELECTRONIC COMPONENT == 1)
{
seconds=15;
} else {
seconds--;
}
} else {
System.out.println("Countdown finished");
CountDown.a=0;
}
}
}
public static void main(String args[])
{
CountDown.a = 0;
while(true)
{
if(READING ELECTRONIC COMPONENT == 1)
{
if (CountDown.a==0)
{
CountDown.a = 1;
new CountDown();
}
}
}
}