如何计算两个用户输入之间的长度(以秒为单位)?

时间:2017-06-01 11:33:55

标签: java eclipse

您如何计算用户第一次按下输入和第二次按下输入之间的时间差。我查看了堆栈溢出问题,显示了两个日期之间的区别,但我不知道在这种情况下如何做到这一点。

int TimeOne;
int TimeTwo;
int Answer;

System.out.print("What is 5 + 10? (Please press enter to start the timer)")
Answer = keyboard.nextInt();

1 个答案:

答案 0 :(得分:0)

根据greg-449的建议,Instant和Duration可以用作:

import java.time.Duration;
import java.time.Instant;

...

Instant start = Instant.now()

// here waiting for user input, very simplified, just an example
System.in.read();

Instant end = Instant.now();
Duration duration = Duration.between(start, end);

然后可以根据需要使用任何持续时间的方法。获取持续时间的示例:

int seconds = duration.getSeconds();