Java 将当前时间保存为变量并从该值继续计数
我正在制作秒表,努力使停止功能正常工作 当我在7秒时停止计时器并重新启动时,它会回到0秒并重新开始;但是,我希望它在7秒开始。有时,秒表会断开并持续显示1.529679177849E9秒 我知道我需要将节省的时间存储在变量中,但我不知道如何存储 你能帮我解决这个问题吗Java 将当前时间保存为变量并从该值继续计数,java,debugging,time,save,Java,Debugging,Time,Save,我正在制作秒表,努力使停止功能正常工作 当我在7秒时停止计时器并重新启动时,它会回到0秒并重新开始;但是,我希望它在7秒开始。有时,秒表会断开并持续显示1.529679177849E9秒 我知道我需要将节省的时间存储在变量中,但我不知道如何存储 你能帮我解决这个问题吗 public class stopwatch { public long thetime = 0; public long stoppedtime = 0; public boolean ticking =
public class stopwatch {
public long thetime = 0;
public long stoppedtime = 0;
public boolean ticking = false;
public static void main(String[] args) {
stopwatch s = new stopwatch();
Scanner sc = new Scanner(System.in);
boolean loop = true;
while (loop = true) {
System.out.println("1 start 2 is started 3 stop 4 "
+ "reset 5 check time 6 stop");
int i = sc.nextInt();
if (i == 1) {
s.start();
} else if (i == 2) {
System.out.println(s.isStarted());
} else if (i == 3) {
s.stop();
} else if (i == 4) {
s.reset();
} else if (i == 5) {
System.out.println("saved time is " + s.time() + " Seconds");
} else if (i == 6) {
System.out.println("closing");
loop = false;
break;
} else {
System.out.println("invalid");
}
}
}
public void start() {
if (ticking == true) {
thetime = thetime;
} else {
thetime = System.currentTimeMillis();
ticking = true;
}
}
public boolean isStarted() {
return ticking;
}
public void stop() {
if (ticking == false) {
stoppedtime = stoppedtime;
} else {
stoppedtime = thetime;
ticking = false;
}
}
public void reset() {
thetime = 0;
stoppedtime = 0;
}
public double time() {
double seconds = 1000.000000;
double currenttime = 0;
double saved = stoppedtime;
if (ticking == true) {
currenttime = ((System.currentTimeMillis() - thetime) / seconds);
return currenttime;
} else {
currenttime = (stoppedtime / seconds);
return currenttime;
}
}
}
我已经解决了一些问题,请查看下面的工作代码: 1) 返回时间时,stoppedtime未转换为秒 2) 只需在实际结果中添加停止时间,以确保在调用重置之前始终添加停止时间 3) 根据编码标准,类名应始终以大写字母开头
public class Stopwatch {
public long thetime = 0;
public long stoppedtime = 0;
public boolean ticking = false;
public int laps = 0;
public long lastTime = 0;
public static void main(String[] args) {
Stopwatch s = new Stopwatch();
Scanner sc = new Scanner(System.in);
boolean loop = true;
while (loop = true) {
System.out.println("1 start 2 is started 3 stop 4 " + "reset 5 check time 6 stop");
int i = sc.nextInt();
if (i == 1) {
s.start();
} else if (i == 2) {
System.out.println(s.isStarted());
} else if (i == 3) {
s.stop();
} else if (i == 4) {
s.reset();
} else if (i == 5) {
System.out.println("saved time is " + s.time() + " Seconds");
} else if (i == 6) {
System.out.println("closing");
loop = false;
break;
} else {
System.out.println("invalid");
}
}
}
public void start() {
if (!ticking) {
thetime = System.currentTimeMillis();
ticking = true;
}
}
public boolean isStarted() {
return ticking;
}
public void stop() {
if (ticking ) {
stoppedtime += (System.currentTimeMillis() - thetime);
ticking = false;
}
}
public void reset() {
thetime = System.currentTimeMillis();
stoppedtime = 0;
}
public double time() {
double seconds = 1000.000000;
double currenttime = 0;
if (ticking) {
currenttime = (stoppedtime + System.currentTimeMillis() - thetime) / seconds;
return currenttime;
} else {
currenttime = (stoppedtime / seconds);
return currenttime;
}
}
}
输出:
1 start 2 is started 3 stop 4 reset 5 check time 6 stop
5
saved time is 0.0 Seconds <-- Starting time to be zero
1 start 2 is started 3 stop 4 reset 5 check time 6 stop
1 <-- Start time
1 start 2 is started 3 stop 4 reset 5 check time 6 stop
3 <-- Stop Time
1 start 2 is started 3 stop 4 reset 5 check time 6 stop
5
saved time is 1.304 Seconds <-- Time of stopwatch
1 start 2 is started 3 stop 4 reset 5 check time 6 stop
1 <- Started time from 1.304 Seconds
1 start 2 is started 3 stop 4 reset 5 check time 6 stop
5
saved time is 1.788 Seconds <- After immediately checking time post start as you can see timer started from previous recorded time
1 start 2 is started 3 stop 4 reset 5 check time 6 stop
3 <- Stop time
1 start 2 is started 3 stop 4 reset 5 check time 6 stop
5
saved time is 4.988 Seconds <--Combined time for Lap 1 + Lap 2
1 start 2 is started 3 stop 4 reset 5 check time 6 stop
4 <- Reset time
1 start 2 is started 3 stop 4 reset 5 check time 6 stop
5
saved time is 0.0 Seconds <- Time reset to 0 Seconds
1 start 2 is started 3 stop 4 reset 5 check time 6 stop
1 <- Start time
1 start 2 is started 3 stop 4 reset 5 check time 6 stop
3 <- Stop time
1 start 2 is started 3 stop 4 reset 5 check time 6 stop
5
saved time is 0.796 Seconds <- Previous lap time not recorded as reset was done
1启动2启动3停止4重置5检查时间6停止
5.
节省的时间是0.0秒,非常感谢您的帮助,但这些更改并没有真正改变代码的功能。@j.doe它符合您的要求。请检查上面的输出,如果您多次启动和停止,记录的时间会被添加,但是如果您重置时间,它会再次将启动时间更改为零。再次测试您的代码,我注意到它有点工作,但如果在不停止的情况下重置计时器,则存在一个问题,保存的时间将永久存储为1.529749753663E9秒。除此之外,它似乎正在起作用。@j.doe要解决这个问题,只需将stop从重置为第一行两种方式都很简单,我简直不敢相信我从未想过,我在时间方法中添加了更多if语句,非常感谢,希望你周末过得愉快。你寻找过类似的问题吗?有一些。