简体   繁体   中英

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.

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.

This will accept ENTER key to be pressed five times and keep intervals for you:

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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