我正在制作秒表并努力使停止功能正常工作。
当我在 7 秒停止计时器并重新启动时,它会回到 0 秒并重新开始;但是,我希望它从 7 秒开始。有时,秒表会中断并持续显示 1.529679177849E9 秒。
我知道我需要将保存的时间存储在一个变量中,但我不知道如何。
你能帮我解决这个问题吗?
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)停止时间在返回时间时没有转换成秒
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句