打字速度测试

打速度测试的小程序:

public class Typer {
    
    private static final String CHAR = "abcdefghijklmnopqrstuvexyz"
        + "ABCDEFGHIJKLMNOPQRSTUVEXYZ1234567890"
        + "`-=[];',./~!@#$%^&*()_+{}:\"<>?";
    
    public static void main(String[] args) throws IOException {
        testTyping();
    }
    
    public static void testTyping() throws NumberFormatException, IOException {
        //读取控制台的输入
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Please input the length of string template:");
        int length = Integer.valueOf(reader.readLine());
        System.out.println("String Templage:");
        String template = getTypingTemplage(length);
        System.err.println(template);
        long startTime = System.currentTimeMillis();
        String input = reader.readLine();
        long endTime = System.currentTimeMillis();
        double correctCount = calculateCorrentNumber(template, input);
        System.out.println("---------------------Result---------------------");
        System.out.println("Corrent Rate: " + Math.round((correctCount / template.length()) * 100) + "%");
        System.out.println("Time: " + Math.round((endTime - startTime) / 1000) + "s");
    }
    
    private static double calculateCorrentNumber(String template, String input){
        int correctCount = 0;
        for (int i = 0; i < template.length(); i++) {
            if (input.length() <= i) break;
            if (template.charAt(i) == input.charAt(i)) {
                correctCount++;
            }
        }
        return correctCount;
    }
    
    private static String getTypingTemplage(int length) {
        String result = "";
        int charSize = CHAR.length();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            int pos = random.nextInt(charSize);
            result += CHAR.charAt(pos);
        }
        return result;
    }
}

你可能感兴趣的:(测试)