📄 calculatespeed.java
字号:
/** This class calculates the Raw Speed,Adjusted Speed, Accuracy and Number of Words Typed.
* It requires a string to compute the accuray,speed etc.
* Its update method is called by a thread to update values timely.
*/
class CalculateSpeed{
private double speed,start,elapsed,minutes;
private boolean flag=true,stopflag;
private long words;
private String user="",actual;
private DialogLabel status;
CalculateSpeed(String actual,DialogLabel status){
this.actual=actual;
this.status=status;
}
public void startLesson(){
if(flag)
start=System.currentTimeMillis();
flag=false;
}
public void update(char ch){
if(ch==' '||ch==10)
words++;
else if(ch==8){
if(user.length()>0)
user=user.substring(0,user.length()-1);
return ;
}
user=user+ch;
update();
}
public void update(){
elapsed=System.currentTimeMillis()-start;
minutes=elapsed/60000;
if(minutes>0)
speed=words/minutes;
if(!status.getText().equals("Press Enter"))
status.setText("Speed="+getRawSpeed()+" WPM");
}
public long getRawSpeed(){
return Math.round(speed);
}
public long getAdjustedSpeed(){
long total=user.length();
if(total==0) return 0;
long accuracy=getAccuracy();
long correct=Math.round(total*accuracy/100);
return Math.round(getRawSpeed()*correct/total);
}
public long getAccuracy(){
long correct=0;
long total=user.length();
if(total==0) return 0;
for(int i=0;i<total;i++)
if(actual.charAt(i)==user.charAt(i))
correct++;
return Math.round(100*correct/total);
}
public long getWords(){
return words;
}
public long getTime(){
return (long)elapsed;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -