1importjava.util.*;23publicclassQuickpress{4publicstaticvoidmain(String[] args)throwsInterruptedException{5Random random =newRandom();6long wait = random.nextInt(50)*100;7Scanner scanner =newScanner(System.in);8System.out.println("Hit enter to start:");9 scanner.nextLine();10Thread.sleep(wait);11System.out.println("*");12long start =System.currentTimeMillis();13 scanner.nextLine();14long end =System.currentTimeMillis();15System.out.println("タイム: "+(end-start)+"[ms]");16}17}
java
1importjava.io.IOException;2importjava.util.Random;34publicclassQuickpress{5publicstaticvoidmain(String[] args)throwsIOException,InterruptedException{6Random random =newRandom();7long wait =(random.nextInt(50)+1)*100;//100~5000(100刻み)8int br =getBr();9System.out.println("Hit enter to start:");10waitTarget(br);11Thread.sleep(wait);12clearStdIn();//sleep中に enter されても無視13System.out.println("*");14long start =System.currentTimeMillis();15waitTarget(br);16long end =System.currentTimeMillis();17System.out.println("タイム: "+(end-start)+"[ms]");18}19//改行コードを取得(Windows は最後のバイト)20staticintgetBr(){21String br =System.getProperty("line.separator");22byte[] bytes = br.getBytes();23return bytes[bytes.length-1];24}25//標準入力のゴミを削除26staticvoidclearStdIn()throwsIOException{27while(System.in.available()!=0)System.in.read();28}29//標準入力から target が入力されるまで待つ30staticvoidwaitTarget(int target)throwsIOException{31while(System.in.read()!= target);32}33}