2017-06-01 132 views
-2

您將如何計算用戶第一次按下輸入和第二次按下輸入之間的時間差。我已經通過堆棧溢出問題了解了兩個日期之間的差異,但我不確定在這種情況下如何去做。如何計算兩個用戶輸入之間的時間(以秒爲單位)?

int TimeOne; 
int TimeTwo; 
int Answer; 

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

將是很難幫助你,如果你不提供[SSCCE(http://sscce.org/)... –

+0

當用戶點擊進入您創建一個日期並獲得用戶折磨的時間,然後,當用戶第二次點擊時,您將該日期與當時的時間進行比較,我認爲這會起作用 –

+1

@FranciscoMelicias不一定是' Date'。用'System.currentTimeMillis()'來測量'long'就足夠了。 – QBrute

回答

0

如所建議的由Greg-449的即時和持續時間可以被用作:根據需要

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(); 
相關問題