代码如下:
[code=Java]import java.io.*;
import java.util.GregorianCalendar;
class Counter{
public void reset(){
count=0;
}
public void increment(){
count+=1;
}
public int getValue(){
return count;
}
private int count=0;
}
class Mini{
public void check(long time){
if(time<miniTime)
miniTime=time;
}
public long getValue(){
return miniTime;
}
public void reset(){
miniTime=92233720368547L;
}
private long miniTime=92233720368547L;
}
class Watch{
public void start(){
GregorianCalendar now=new GregorianCalendar();
startTime=now.getTimeInMillis();
}
public void showElapsedTime(){
GregorianCalendar now=new GregorianCalendar();
long currentTime=now.getTimeInMillis();
long difference=(currentTime-startTime)/1000;
System.out.print(difference);
System.out.println("seconds hava elapsed.");
}
public long getElapsedTime(){
GregorianCalendar now=new GregorianCalendar();
long currentTime=now.getTimeInMillis();
return (currentTime-startTime);
}
private long startTime;
}
class TypingTutor{
public long test() throws Exception{
BufferedReader keyboard=new BufferedReader(
new InputStreamReader(System.in));          //只有是用到输入输出流,就必须进行throws Exception
long testTime;
Watch sw=new Watch();
System.out.println("Hit Enter to start!,practiceString:"+practiceString);
String inputString=keyboard.readLine();
System.out.println("Type!");
sw.start();
inputString=keyboard.readLine();
testTime=sw.getElapsedTime();
if(practiceString.equals(inputString)){
prefectCount.increment();
return testTime;
}
else return -1;
}
public long getLowestTime(){
return lowestTime.getValue();
}
public long getNumPrefect(){
return prefectCount.getValue();
}
public void setPractieString(String newString){
practiceString=newString;
prefectCount.reset();
lowestTime.reset();
}
private String practiceString="The quick brown fox jumps over the lazy dog.";
private Counter prefectCount=new Counter();
private Mini lowestTime=new Mini();
}
public class Test {
public static void main(String[] arg)throws Exception{     //即使是调用的函数里面有buffer,也要throws Exception
TypingTutor t=new TypingTutor();
System.out.println("First test:");
long result=t.test();
if(result>0){
System.out.println("your time is: ");
System.out.println(result/1000+"seconds");
}else
System.out.println("You did not type the correct number!");
System.out.println();
System.out.println("Second test:");
result=t.test();
if(result>0){
System.out.println("your time is: ");
System.out.println(result/1000+"seconds");
}else
System.out.println("You did not type the correct number!");
System.out.println();
System.out.println("Third test:");
result=t.test();
if(result>0){
System.out.println("your time is: ");
System.out.println(result/1000+"seconds");
}else
System.out.println("You did not type the correct number!");
System.out.println();
if(t.getNumPrefect()>0){
System.out.println("Your lowestTime is: ");
System.out.println(t.getLowestTime());
}else
System.out.println("You failed in three times!");
t.setPractieString("Exotic zebras jaywalk with impunity.");      //这里改变字符串值
System.out.println("First test:");
result=t.test();
if(result>0){
System.out.println("your time is: ");
System.out.println(result/1000+"seconds");
}else
System.out.println("You did not type the correct number!");
System.out.println();
System.out.println("Second test:");
result=t.test();
if(result>0){
System.out.println("your time is: ");
System.out.println(result/1000+"seconds");
}else
System.out.println("You did not type the correct number!");
System.out.println();
System.out.println("Third test:");
result=t.test();
if(result>0){
System.out.println("your time is: ");
System.out.println(result/1000+"seconds");
}else
System.out.println("You did not type the correct number!");
System.out.println();
if(t.getNumPrefect()>0){
System.out.println("Your lowestTime is: ");
System.out.println(t.getLowestTime());
}else
System.out.println("You failed in three times!");

}}
/code]补充说明:TypingTutor类,这个类的核心部分test()方法,显示一个测试字符串,而且要求用户回打,当用户输入这个字符串的时候,该方法会计时,因此当用户输入完毕之时,程序会告诉用户说有没有正确输入该字符串,或者是给出用户输入一共花费了多少秒。同时3次后,主函数会变化字符串,要求用户重新输入,重新测试,同时会显示3次最短正确输入的时间。顺便问下,究竟java里是不是很少用循环啊?