日期:2014-05-20 浏览次数:20807 次
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); } }