Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Revisions

8 of 8
Commonmark migration

Java, (削除) 404 (削除ここまで) (削除) 388 (削除ここまで) (削除) 354 (削除ここまで) (削除) 348 (削除ここまで) (削除) 320 (削除ここまで) 318 bytes

import java.awt.*;import java.awt.event.*;interface M{static void main(String[]a){new Frame(){{add(new TextArea(){{addKeyListener(new KeyAdapter(){long t,i=64;public void keyPressed(KeyEvent e){t=t>0?t:e.getWhen();if(e.getKeyChar()!=++i|i>89){System.out.print(i>89?e.getWhen()-t:"Fail");dispose();}}});}});show();}};}}

And here I thought Java Console was already verbose..
Since Java has no way to raw listen for key-presses in the Console a.f.a.i.k., I use a GUI with java.awt.

-78 bytes thanks to @OlivierGrégoire.

Explanation:

import java.awt.*; // Required import for Frame and TextField
import java.awt.event.*; // Required import for KeyAdapter and KeyEvent
interface M{ // Class
 static void main(String[]a){ // Mandatory main-method
 new Frame(){ // Create the GUI-Frame
 { // With an initialization-block
 add(new TextArea(){ // Add an input-field
 { // With it's own initialization-block
 addKeyListener(new KeyAdapter(){
 // Add a KeyAdapter to the input-field
 long t, // Long to save the time
 i=64; // Previous character, starting at code of 'a' -1
 public void keyPressed(KeyEvent e){ 
 // Override the keyPressed-method:
 t=t>0? // If `t` is already set:
 t // Leave it the same
 : // Else:
 e.getWhen(); // Save the current time (== start the timer)
 if(e.getKeyCode()!=++i
 // As soon as an incorrect character is pressed,
 |i>89){ // or we've reached 'z':
 System.out.print(i>89?
 // If we're at 'z':
 e.getWhen()-t // Print the end-time in ms to the Console
 : // Else (an incorrect character was pressed)
 "Fail"); // Print "Fail" to the Console
 dispose();} // And exit the application
 } // End of keyPressed-method
 }); // End of KeyAdapter
 } // End of input-field initialization-block
 }); // End of input-field
 show(); // Initially show the Frame
 } // End of Frame initialization-block
 }; // End of Frame 
 } // End of main-method
} // End of class

Example gif of success: (Yes, I type the alphabet pretty slowly here..)
Note: This is an old gif. Current version no longer prints key-presses to Console. And it no longer prints the time with digits after the decimal point.

enter image description here
Example gif of fail:
Note: This is an old gif. Current version no longer prints key-presses to Console.

enter image description here

Kevin Cruijssen
  • 136.2k
  • 14
  • 154
  • 394

AltStyle によって変換されたページ (->オリジナル) /