简体   繁体   English

如何使用计时器类

[英]How to use timer class

I'm a novice in Java programming and I wanted to implement the timer class in my program. 我是Java编程的新手,我想在程序中实现timer类。 I'm making a mini time-based testing system which outputs a question then time remaining and then the answer choices Example: 我正在制作一个基于时间的小型测试系统,该系统输出一个问题,然后剩余时间,然后给出答案选择。示例:

  1. Which operator returns the remainder of integer division? 哪个运算符返回整数除法的余数? (4 minutes left...) (还剩4分钟...)

A. % B./ C. * D. None of the above A.%B. / C. * D.以上都不是

  1. The escape sequence that represents the new line character is: (1 minutes left...) 代表换行符的转义序列为:(还剩1分钟...)

A. \\r B. \\t C. \\n D. \\ A. \\ r B. \\ t C. \\ n D. \\

I don't know how to get that time remaining part. 我不知道该如何剩余时间。 It's supposed to run in background and change right when the next question comes up 它应该在后台运行并在下一个问题出现时立即更改

You can accomplish what you're trying to do without using timer, which will probably be easier, as you're new. 您无需使用计时器就可以完成您想做的事情,这可能会更容易,因为您是新手。

There's a function called System.currentTimeMillis() that returns the current system time in milliseconds. 有一个名为System.currentTimeMillis()的函数,该函数以毫秒为单位返回当前系统时间。 From that, you can: 由此,您可以:

  1. Store the time when you start 储存开始时间
  2. Do some other stuff 做其他事情
  3. Check the time when you stop, and calculate the time remaining 检查停止时间,并计算剩余时间

As such: 因此:

public static void main(String[] args) {
    long startTime = System.currentTimeMillis();

    // Do other stuff...

    long stopTime = System.currentTimeMillis();
    int elapsedSeconds = (int)((stopTime - startTime) / 1000);
    System.out.println(elapsedSeconds + " seconds elapsed");
}

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

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