简体   繁体   English

我该如何编写一个Java程序来获得两次键盘按键之间的时间?

[英]How do I write a Java program that can get the time between my keyboard key-presses?

So my main objective is to create a new Random number generating function that will generate a random number with the help of human randomness. 因此,我的主要目标是创建一个新的随机数生成函数,该函数将在人类随机性的帮助下生成一个随机数。 I want to be able to get the time between previous keystrokes that were entered by the user and save them in some sort of a float or double variable. 我希望能够获得用户输入的先前击键之间的时间,并将它们保存在某种float或double变量中。

While trying to create such a program, I thought about using a pointer to get the address of a variable since the address is random and thought I could proceed with that. 在尝试创建这样的程序时,我考虑过使用指针来获取变量的地址,因为该地址是随机的,因此我认为可以继续进行下去。 But apparently Java doesn't support pointers so here I am, trying to get a new idea for generating random numbers. 但是显然Java不支持指针,所以我在这里试图获得生成随机数的新思路。

This will accept ENTER key to be pressed five times and keep intervals for you: 这将接受五次按ENTER键并为您保持间隔:

Scanner scanner = new Scanner(System.in);
double[] randomIntervals = new double[5];
for (int i = 0; i < 5; i++) {
  long start = System.currentTimeMillis();
  scanner.nextLine();
  long end = System.currentTimeMillis();
  randomIntervals[i] = end - start;
}
System.out.println(Arrays.toString(randomIntervals));
scanner.close();

Here is a sample run for me: 这是为我运行的示例:

[1307.0, 523.0, 660.0, 165.0, 165.0]

Does this help? 这有帮助吗?

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

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