好吧,所以我正在构建一个时钟,它会以 Log10(小时)的形式给出时间,并以相同的格式给出分钟和秒,但是当我这样做时,我不断收到奇怪的输出运行。我得到的大部分是 NAN,有时是无穷大(或其负值)。我有一种感觉,我使用的数字有问题(比如它们可能太大或类似的东西)注意:currentTime 是一个 long,calendar 是一个 Calendar,而小时、分钟和秒是 double 的。时钟变量也是一个 JLabel。程序经过两次迭代后,它总是切换到 NAN。
this.timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
currentTime = System.currentTimeMillis();
calendar.setTime(new Date(currentTime));
hour = Math.log10(hour);
minute = Math.log10(minute);
second = Math.log10(second);
System.out.println(hour + " " + minute + " " + second);
return null;
}
@Override
protected void done() {
clock.setText("<html><body><p>Log Time: "
+ "Log<sub>10</sub>(" + hour + ") || "
+ "Log<sub>10</sub>(" + minute + ") || "
+ "Log<sub>10</sub>(" + second + ")</p></body></html>");
}
}.execute();
}
}, 0, 200);
最佳答案
这段代码没有多大意义:
currentTime = System.currentTimeMillis();
calendar.setTime(new Date(currentTime));
hour = Math.log10(hour);
minute = Math.log10(minute);
second = Math.log10(second);
您正在日历
中设置时间 - 但随后完全忽略它。我希望你会想要这样的东西:
currentTime = System.currentTimeMillis();
calendar.setTime(new Date(currentTime));
hour = Math.log10(calendar.get(Calendar.HOUR_OF_DAY));
minute = Math.log10(calendar.get(Calendar.MINUTE));
second = Math.log10(calendar.get(Calendar.SECOND));
编辑:正如评论中所述,您还需要考虑当小时、分钟或秒为 0 时您将做什么。(非常不清楚为什么您想要使用日志说实话,首先是基于时钟的。)
关于Java 对数时钟给出不准确的答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17964891/