gpt4 book ai didi

Java - 程序未更新 system.currentTimeMillis()

转载 作者:行者123 更新时间:2023-12-01 08:05:29 24 4
gpt4 key购买 nike

public class Clock {
int second;
int minute;
int hour;
boolean checkTime = true;
float startTime, endTime, difference;
public void time(){
if(checkTime){
startTime=System.currentTimeMillis();
checkTime=false;
}
endTime=System.currentTimeMillis(); // THIS ISN'T UPDATING, keeps the same
//value as start value each run through.
difference=endTime-startTime;

if(difference >= 1000){
second++;
checkTime = true;
}
}
public int getSecond(){
return second;
}
public int getMinute(){
return minute;
}
public int getHour(){
return hour;
}
public int getMs() {
return (int)difference;
}
}

这是我试图创建的时钟类,以便跟踪时间。我有一个主类,它处于无限循环中,不断调用 time(); 。 void 方法 time 应该更新每次运行的时间,但它拒绝更新 endTime。 startTime 将获得一个初始值,然后相同的值将一遍又一遍地赋予 endTime。有人可以解释为什么会发生这种情况吗? *差异代表毫秒

最佳答案

System.currentTimeMillis(); 返回一个 long 并且您将其分配给 endTime(一个 float) 。使用长startTime、endTime、差值;

关于Java - 程序未更新 system.currentTimeMillis(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22056183/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com