java关于打字校验正确率的实现

这是一个Java程序,用于加载指定的TXT文件并进行打字校验,计算输入文字与原文本的正确率。用户可以选择不同的文章,程序会读取文件内容,接收用户输入,然后计算并显示打字的正确率和耗时。

package oop.kechengsheji;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;

/**加载文件*/
public class ReWord{
 private String str; 
 public  ReWord(int k){
  ArrayList<String> list=new ArrayList<String>();
  String str;
  switch(k){//选择要打的文章用txt保存
  case 0:str="F:\\word.txt";this.str=str;break;
  case 1:str="F:\\word0.txt";this.str=str;break;
  case 2:str="F:\\word1.txt";this.str=str;break;
  case 3:str="F:\\word2.txt";this.str=str;break;
  }
  try{//用文件流读取文本内容
   InputStreamReader isr=new InputStreamReader(new FileInputStream(this.str)); 
   BufferedReader reader=new BufferedReader(isr);
   String line=reader.readLine();
   if(line!=null){//如果内容不为null按行读取
    //System.out.println(line);
    list.add(line);
    System.out.println(list);
   }
   char[] c2=line.toCharArray();
   Scanner scanner=new Scanner(System.in);
   long start=System.currentTimeMillis();
   System.out.println("开始打字:");//输入文字
   String str2=scanner.nextLine();
   double count=0;
   //int count1=0;
   char[] c1=str2.toCharArray();
   for(int index=0;index<Math.min(c1.length,c2.length);index++){
    if(c1[index]==c2[index]){//比较你所打字与原文字体相同计数器加1
     count++;
    }
   /*if(str2.charAt(index)==line.charAt(index)){
     count++;
    }*/
   }
   long ends=System.currentTimeMillis();
   System.out.println("共耗费时间:"+(ends-start)/(60*1000)
     +"分"+(ends-start)/1000+"秒"+(ends-start)%1000+"毫秒");//输出你所花费时间
   double total=count/c1.length;
   System.out.println("正确率:"+total*100+"%");//输出你打字的正确率
  }catch (Exception e){
   e.printStackTrace();
  }
 }
}

/**调试程序*/

package oop.kechengsheji;

import java.util.Scanner;

public class Wor{
  public static void main(String[] argus){
  Scanner scanner=new Scanner(System.in);
  while(true){
   System.out.println("请输入你要选择的文件(0-3):");
   String str=scanner.nextLine();   
   int k=Integer.parseInt(str);
   ReWord reword=new ReWord(k);
   System.out.println(reword);   
  }
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值