【发布时间】:2016-02-25 17:16:30
【问题描述】:
所以,我一直试图弄清楚这个计时器是如何工作的,现在我终于让它编译没有错误,我似乎无法让它正常运行,我假设是因为我' m 没有给它正确的 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();
}
}
【问题讨论】:
-
如果你不知道它应该是什么,我怀疑你自己写了这段代码......你尝试过作为参数传递什么?
-
我从来没有说过我自己写的,我只是想弄清楚如何让它工作以帮助我更好地理解这里发生的一切。我试过输入“java Clock int int" "java Clock int" 甚至是 "java Clock string 它在主 java.time.etc 中给了我一个异常ParseException: int 无法在索引 #index 中解析
-
你传递给它的实际值是什么,你希望它做什么,它会做什么?
-
您究竟希望如何将“int”解析为 LocalTime ? docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html
-
也许阅读
LocalTime.parse的 Javadoc,它提供了示例输入。
标签: java timer command-line-arguments