我希望用户回答一个问题,但是他们的输入是从提示符到按enter键的时间。然后根据他们花了多长时间,我想把这个问题和答案放在某个地方。
Question1:牢房的动力是什么?答:线粒体答案是正确的,用户花了6秒按回车。
将Question1存储到A甲板(因为所有的t<10秒都应该在A甲板上)
问题2:心肌中的炎症叫什么?答:心肌炎答案正确,用户花了15秒按回车。
将Question2储存在C甲板(因为所有t >=15秒都应在C甲板上,但小于20秒)
我可以为这个问题编写所有的程序,只是我似乎无法根据这个时间来存储用户输入。任何帮助都是很好的,谢谢!
发布于 2022-02-23 10:42:28
我认为您只需在读取用户输入之前和之后节省时间。我为你的一个问题创造了一个小例子:
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // Create a Scanner object
System.out.println("What is the powerhouse of the cell?"); // First question
long startTime = System.currentTimeMillis(); // Save start time
String answer = scanner.nextLine(); // Read user input
long endTime = System.currentTimeMillis(); // Save time after enter
long questionTime = (endTime - startTime) / 1000; // Calculate difference and convert to seconds
if (answer.equals("Mitochondria")) { // Check if answer is correct and print output
System.out.println("The answer is correct, and user took " + questionTime + " seconds to press enter");
} else {
System.out.println("The answer is wrong, and user took " + questionTime + " seconds to press enter");
}
}
}
控制台日志:
What is the powerhouse of the cell?
Mitochondria
The answer is correct, and user took 3 seconds to press enter
发布于 2022-02-23 10:51:28
使用Instant.now()
;捕获当前时间(
在按enter键后,立即使用Instant.now()
;捕获当前时间。
Duration::between
获取这两个瞬间之间的持续时间.Duration
格式化为所需的格式。d.toMillisPart()) (%ss和%sms),d.getSeconds(),String.format
https://stackoverflow.com/questions/71235250
复制相似问题