0

我有一门课叫时钟。它计算所花费的时间。在我的项目中,需要使用构造函数来初始化开始时间。当我使用构造函数时,它最终总是给我时间 = 0.0 毫秒。有人可以帮我解决这个问题吗?

时钟.java

public class Clock {

private long start;

public Clock() {
    start = 0;
}
public Clock(String s) {
    start = s;
}

public void start() {
    start = System.currentTimeMillis();
}

public double stop() {
    long stop = System.currentTimeMillis();
    double time = stop - start;
    return time;
}

}

我在另一个类中调用的部分代码。c 是从类 Clock.java 实例化的对象

    public static void insertion(Clock c, Sort s, Scanner sc) throws IOException {
    for (int count = 0; count < 11; count++) {
        String path[] = selectPath(count);
        String filepath = path[0];
        int num = Integer.parseInt(path[1]);
        int[] NumArrays = populate(filepath, num);
        c.start();
        s.insertionSort(NumArrays);
        double timeI = c.stop();
        System.out.println("Insertion sort took " + timeI + "ms for " + path[1] + " data");
    }
    menu(sc, c, s);
}
4

0 回答 0