谁能给我一个关于在 Java 中保存整数的快速提示。我正在使用计时器制作自定义时钟,并希望在我退出程序时保存时间(或启动自动重启并调用 save 方法)并从停止的地方开始。
我的代码:
MainClass 中的方法:
t = new Timer();
tt = new JGameTimer();
t.scheduleAtFixedRate(tt, 1000, 1000);
Tmer类:
private static int timeInSeconds = 0, timeInMinutes = 0, timeInHours = 0, timeInDays = 0, timeInYears = 0;
private int lastTimeInSeconds, lastTimeInMinutes, lastTimeInHours, lastTimeInDays, lastTimeInYears;
private boolean timeHasBeenSet = false;
public void run() {
if (!timeHasBeenSet) {
timeInSeconds = lastTimeInSeconds;
timeInMinutes = lastTimeInMinutes;
timeInHours = lastTimeInHours;
timeInDays = lastTimeInDays;
timeInYears = lastTimeInYears;
timeHasBeenSet = true;
}else{
continueTime();
}
}
public void saveTime() {
lastTimeInSeconds = timeInSeconds;
lastTimeInMinutes = timeInMinutes;
lastTimeInHours = timeInHours;
lastTimeInDays = timeInDays;
lastTimeInYears = timeInYears;
}
public void continueTime() {
timeInSeconds++;
if (timeInSeconds == 60) {
timeInSeconds = 0;
timeInMinutes++;
}
if (timeInSeconds == 10) {
saveTime();
System.out.println("Time Saved (Seconds)");
}
if (timeInMinutes == 60) {
timeInMinutes = 0;
timeInHours++;
}
if (timeInMinutes == 2) {
saveTime();
System.out.println("Time Saved (Minutes)");
}
if (timeInHours == 24) {
timeInHours = 0;
timeInDays++;
}
if (timeInDays == 2920) {
timeInDays = 0;
timeInYears++;
}
System.out.println("Time is "+timeInYears+":"+timeInDays+":"+timeInHours+":"+timeInMinutes+":"+timeInSeconds+"");
}