简体   繁体   English

Java System.currentTimeMillis()问题

[英]Java System.currentTimeMillis() problem

So in my java class we have a homework assignment to use System.currentTimeMillis to display the amount of time between clicks. 所以在我的java类中,我们有一个家庭作业,使用System.currentTimeMillis来显示点击之间的时间量。 I've tried and tried but it isn't working. 我尝试过但尝试过但是没有用。 Here's my code. 这是我的代码。

 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 }

I don't really want anyone to completely tell me how to do it but I just need a little guidance. 我真的不想让任何人完全告诉我该怎么做但我只需要一些指导。 What am I doing to make this wrong? 我做错了什么? The code all compiles and runs just fine but when I click instead of giving me the time that elapsed between clicks it's giving me a big long number that never changes. 代码全部编译并运行得很好,但是当我点击而不是给我点击之间经过的时间时,它给了我一个永远不会改变的大数字。 It's telling me 1.316639174817E9 almost every single time. 它几乎每次都告诉我1.316639174817E9。

Firstly, system time in millis should be represented as a long, not a double. 首先,以毫秒为单位的系统时间应表示为长,而不是双倍。

Secondly, you need to make the variable that holds the time since the last click (timerS) an instance variable, it's currently method local and so reset every time. 其次,你需要创建一个变量,它保存自上次单击(timerS)以来的时间实例变量,它当前是本地方法,因此每次都重置。

In short, change: 简而言之,改变:

double timerS=0;

from being a local variable, to an instance variable, and a long: 从局部变量到实例变量和long:

public class ElapsedTimeClient extends WindowController { 
long timerS;

the timerS variable is declared inside of the onMouseClick method, and thus only exists within this method. timerS变量在onMouseClick方法声明,因此只存在于此方法中。 After your first mouse click, the variable disappears and can't be used to compare times. 第一次单击鼠标后,该变量消失,无法用于比较时间。

Instead you should use a class variable to store this information in. 相反,您应该使用类变量来存储此信息。

Your problems are: 你的问题是:

  • timerS should be a field of the class (not a local variable) otherwise its value will not be held between calls to your method timerS应该是类的一个字段 (不是局部变量),否则它的值将不会在对方法的调用之间保持
  • timerS should be of type long - the type that's returned from the system time timerS应为long类型 - 从系统时间返回的类型

Also: 也:

  • count should be type int count应该是int类型

In addition to the other answers mentioned, System.nanoTime is the preferred method for this type of measurement. 除了提到的其他答案之外, System.nanoTime是此类测量的首选方法。 Rather than measuring the difference in "clock time", this method measured the number of nanoseconds. 该方法测量的是纳秒数,而不是测量“时钟时间”的差异。 You will often find that the resolution on currentTimeMilils is no better than around 16 ms, whereas nanoTime can resolve much smaller intervals. 您经常会发现currentTimeMilils的分辨率不会超过16 ms左右,而nanoTime可以解析更小的间隔。

You don't need double type for timerS. timerS不需要double类型。 currentTimeMillis() returns long. currentTimeMillis()返回long。

Why this happend? 为什么会这样? If you use double type like this: 如果你使用这样的双重类型:

(double - long) / 1000, 

this is interpreted on: 这被解释为:

double / double = double 

So in the result you have "precise" value (eg 1.316639174817E9) instead of long one. 所以在结果中你有“精确”值(例如1.316639174817E9)而不是长值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM