所以在我的java类中,我们有一个作业分配,使用System.currentTimeMillis
来显示点击之间的时间量。我试过并尝试过,但它不起作用。这是我的代码。Java System.currentTimeMillis()问题
1 /* Matthew Caldwell
2 * September 21, 2011
3 * Homework #4: Problem 5.8.1 pg. 149
4 * Program that displays the amount of time that passed
5 * between two mouse clicks. Nothing is displayed
6 * on the first click, then each successive click will
7 * display how much time passed between that click and
8 * the previous one.
9 */
10
11 import objectdraw.*;
12 import java.awt.*;
13
14 public class ElapsedTimeClient extends WindowController {
15
16 public static void main(String[]args) {
17 new ElapsedTimeClient().startController(800,500);
18 }
19
20 private Text title,result;
21 private double count = 0;
22
23 public void begin() {
24
25 // Set up the title and result
26 title = new Text("CLICK COUNTER",
27 canvas.getWidth()/2,
28 20, canvas);29 title.move(-title.getWidth()/2, 0);
30 result = new Text("", canvas.getWidth()/2, 40, canvas);
31
32 }
33
34 public void onMouseClick(Location p) {
35
36 double timerS=0;
37
38 if(count == 0) {
39 timerS = System.currentTimeMillis();
40 } else {
41 result.setText("The time between clicks was " +
42 (System.currentTimeMillis() - timerS)/1000 +
43 " seconds.");
44 timerS = System.currentTimeMillis();
45 }
46
47 count++;
48
49 }
50
51 }
我真的不想让任何人完全告诉我该怎么做,但我只需要一点指导。我在做什么使这个错误? 代码全部编译并运行得很好,但是当我点击而不是给我点击之间流逝的时间时,它给了我一个永不改变的很长的数字。它几乎每一次都在告诉我1.316639174817E9。
啊,我什至没有想到这一点。我假定它在那种方法中被使用,并且只能用它可以呆在那里的方法。此外,我们班的文字说,长期可以存储为一个双,所以我去了。谢谢你清理那个。我会尝试这些,并让你知道它是如何发生的。 – CaldwellYSR
完美运作。非常感谢你。我会给你投票,但我没有足够的代表。 – CaldwellYSR
@CaldwellYSR - np,你能标记答案是正确的吗? – Joel