所以,我一直试图弄清楚这个计时器是如何工作的,现在我终于让它编译没有错误,我似乎无法让它运行正常,我假设因为我'我没有给它适当的cmd ln args但我不知道提供什么args。代码如下:
import java.time.*;
public class Clock {
private LocalTime startTime;
private LocalTime stopTime;
private Duration duration;
private int hours, minutes, seconds;
// no argument constructor that initializes the startTime to the current time
public Clock() {
startTime = LocalTime.now();
}
public Clock(LocalTime start, LocalTime stop) {
startTime = start;
stopTime = stop;
}
//public Clock(start, stop) {
//}
// resets the startTime to the given time
public void start(int h, int m, int s) {
hours = ((h >= 0 && h < 24) ? h : 0);
minutes = ((m >= 0 && m < 60) ? m : 0);
seconds = ((s >= 0 && s < 60) ? s : 0);
startTime = LocalTime.of(hours, minutes, seconds);
}
//a stop() method that sets the endTime to the given time
public void stop(int h, int m, int s) {
hours = ((h >= 0 && h < 24) ? h : 0);
minutes = ((m >= 0 && m < 60) ? m : 0);
seconds = ((s >= 0 && s < 60) ? s : 0);
stopTime = LocalTime.of(hours, minutes, seconds);
}
//a getElapsedTime() method that returns the elapsed time in seconds
public Duration getElapsedTime() {
System.out.println("Difference is " + Duration.between(stopTime, startTime).toNanos()/1_000_000_000.0 + " Seconds.");
duration = Duration.between(stopTime, startTime);
return duration;
}
public static void main(String[] args) {
LocalTime argOne;
LocalTime argTwo;
argOne = LocalTime.parse(args[0]);
argTwo = LocalTime.parse(args[1]);
Clock clockOne = new Clock(argOne, argTwo);
clockOne.getElapsedTime();
}
}
答案 0 :(得分:0)
成功编译后,命令行参数应为这种格式,(包括调用java类)所以我的看起来像:
java Clock 03:14:43 03:17:00 || java时钟03:14 03:17
预期输出:以秒为单位的经过时间:180