大家都玩玩,今天刚写完的import java.util.Random;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
/*
 * author:JayYounger
 */
public class TanTan {
//用户产生随机数
public final static  Random random = new Random();
//输出的字符串,用户给用户提供一个打印模板
public final static StringBuffer sb = new StringBuffer("");
//每次都会让用户输入的数+1,用作统计用户输入的数
private static int charCounter = 1; 
//设定输入时间,因为游戏叫做"是男人就坚持3秒",所以默认值是3
private static final int cancelSecond = 3;
//设定闯关成功字符数
private static final int OK = 10;
public static void main(String[] args) {
new TanTan().start();
}
public void start(){
final Scanner scanner = new Scanner(System.in);
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
long startSecond = System.currentTimeMillis();
for(int i=0;i<charCounter;i++){
int randNum=random.nextInt(26);
char character =(char)(randNum+'A');
sb.append(character);
}
System.out.println(sb);
while(true){
String inputString = scanner.next();
//如果输入时间超过三秒,证明你的坚持力不给力
if(System.currentTimeMillis()-startSecond>cancelSecond*1000){
System.out.println("连"+cancelSecond+"秒都坚持不了,还算不算男人");
System.exit(0);
}
//如果用户输入正确,进入下一关
if(inputString.toUpperCase().equals(sb.toString())){
if(charCounter == OK){
System.out.println("恭喜闯关成功");
System.exit(0);
}
System.out.println("Go next level");
charCounter++;
sb.delete(0,sb.length());
break;
//否则,提示继续闯关
}else{
startSecond = System.currentTimeMillis();
System.out.println("Go on to write it");
System.out.println(sb);
}
}
}
},0, 1000);
}}

解决方案 »

  1.   

    在dos里运行不了,在eclipse中只有几个字母啊,懂的大虾能否讲下原因
      

  2.   

      LZV5,LV你通关了没?我打的手都抽了。
      

  3.   

    好像出了BUG,我写了好多个SUSP还不结束 !  
      

  4.   

    无法编译啊
    The public type TanTan must be defined in its own file
      

  5.   

    CTRL+C  CTRL+V  顺利通过!bug....
      

  6.   

    小意见!lz比我强!不过建议不要用whlie(true) 中间可以加一个sleep 让出cpu 否则cpu占用很大的!
      

  7.   

    印象中学习java第一个类好像就是scanner.
      

  8.   

    这个还有一点bug、如果要通关可以钻牛角尖、因为我发现如果很快的输入错误的、然后回车、只是提示重新输入、然后重新计算时间、如果反复输错、用该时间来浏览要输入的字符串然后可以达到快速输入的目的、