我正在制作秒表,努力使停止功能正常工作。
当我在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;
}
相关分类