我对此很陌生,正在构建一个简单的时钟/计时器程序。使用方法设置时间,获取时间和启动时钟非常简单。一切正常,甚至是计时器。唯一的问题是当计时器首次启动时,它会产生一条错误消息:
"线程中的异常" main" java.lang.IllegalStateException:任务已经>计划或取消 在java.util.Timer.sched(Timer.java:401) at java.util.Timer.scheduleAtFixedRate(Timer.java:328) 在Clock.start(Clock.java:53) 在ClockTest.main(ClockTest.java:46) 时间是:5:55:45 时间是:5:55:46 时间是:5:55:47 时间是:5:55:48 时间是:5:55:49 时间是:5:55:50 时间是:5:55:51"
根据我的理解,这意味着已经启动了另一个计时器,但我不知道我的代码中的位置。为了澄清,当我启动计时器时会产生这个错误,但随后计时器工作。
这是我的代码。这是主要方法:
import java.util.Scanner;
public class ClockTest{
public static void main (String [] args){
int userInput, hr, min, sec;
Clock clock = new Clock();
Scanner scan = new Scanner(System.in);
System.out.println("What would you like to do?\n1) Set Clock"
+ "\n2) Get the time the clock is set to \n3) Start the timer\n"
+ "4) Exit");
userInput=scan.nextInt();
while (userInput!=4){
if (userInput==1){
System.out.println("Enter the hour: ");
hr=scan.nextInt();
System.out.println("Enter the minutes: ");
min=scan.nextInt();
System.out.println("Enter the seconds: ");
sec=scan.nextInt();
clock.setTime(hr,min,sec);
System.out.println("What would you like to do?\n1) Set Clock"
+ "\n2) Get the time the clock is set to \n3) Start the timer\n"
+ "4) Exit");
userInput=scan.nextInt();
}if(userInput==2){
System.out.println(clock.getTime());
System.out.println("What would you like to do?\n1) Set Clock"
+ "\n2) Get the time the clock is set to \n3) Start the timer\n"
+ "4) Exit");
userInput=scan.nextInt();
}if(userInput==3)
clock.start();
}
}
}
这是我创建的Clock类:
import java.util.Timer;
import java.util.TimerTask;
public class Clock {
int seconds=0;
int hours=0;
int minutes=0;
TimerTask hourTask = new TimerTask(){
public void run() {
seconds++;
if(seconds==60 && minutes!=60 && hours!=60){
seconds=0;
minutes++;
}if(seconds==60 && minutes ==60 && hours!=60){
seconds=0;
minutes=0;
hours++;
}if(seconds==60 && minutes==60 && hours==60){
seconds=0;
minutes=0;
hours=0;
}System.out.println("The time is: " + hours + ":" + minutes + ":" + seconds);
}
};
//---------------------------------------------------------------------
//constructor without parameters
//---------------------------------------------------------------------
public Clock (){
hours=0;
minutes=0;
seconds=0;
}
//---------------------------------------------------------------------
//constructor with parameters
//---------------------------------------------------------------------
public Clock (int hrs, int min, int sec){
hours=hrs;
minutes=min;
seconds=sec;
}
//---------------------------------------------------------------------
//starts the clock
//---------------------------------------------------------------------
public void start(){
Timer hourTimer = new Timer();
hourTimer.scheduleAtFixedRate(hourTask, 1000, 1000);
}
//---------------------------------------------------------------------
//sets the time
//---------------------------------------------------------------------
public void setTime(int hrs, int min, int sec){
hours=hrs;
minutes=min;
seconds=sec;
}
//---------------------------------------------------------------------
//gets the time
//---------------------------------------------------------------------
public String getTime(){
return "The time is: " + hours + ":" + minutes + ":" + seconds;
}
}
我对这一切都很陌生,所以如果有人能回答这个问题,我会非常感激。此外,如果您看到我可以改进或清理我的代码,我欢迎所有批评。
另外 - fyi - 我仍然在TextEdit中编写所有内容(我的教授在艰难的学习上很重要),所以如果它看起来不太漂亮。
谢谢