QuickHit项目

首先创建Game类


package cn.play1;

import java.util.Random;

public class Game {
 private Player player;
 public Game (Player player){
  this.player=player;
 }
 public String printStr(){
  int strLength = LevelParam.levels[player.getLevelNo()-1].getStrLength();
  StringBuffer  buffer=new StringBuffer();
  Random random=new Random();
  for(int i=0;i<strLength;i++){
   //产生随机数
   int rand=random.nextInt(strLength);
   //根据随机数拼接字符串
   switch(rand){
   case 0:
    buffer.append(">");
    break;
   case 1:
    buffer.append("<");
    break;
   case 2:
    buffer.append("*");
    break;
   case 3:
    buffer.append("&");
    break;
   case 4:
    buffer.append("%");
    break;
   case 5:
    buffer.append("#");
    break;
   }
   
  }
  System.out.println(buffer);
  return buffer.toString();
 }
 public void printResult(String out,String in){
  boolean flag;
  if(out.equals(in)){
   long currentTime=System.currentTimeMillis();
   //如果超时
   if((currentTime-player.getStartTime())/1000>LevelParam.levels[player.getLevelNo()-1].getTimeLimit()){
    System.out.print("你输入太慢了,已经超时,退出!");
    System.exit(1);
    //如果没有超时
   }else {
     //计算玩家当前积分
    player.setCurScore(player.getCurScore()+LevelParam.levels[player.getLevelNo()-1].getPerScore());
    //计算玩家以用时间
                player.setElapsedTime((int) ((currentTime - player .getStartTime()) / 1000));
                //输出玩家当前级别,当前积分和以用时间
                System.out.println("输入正确,您的级别"+player.getLevelNo()+",您的积分"+player.getCurScore()+",已用时间"+player.getElapsedTime()+"秒");
                //判断用户是否已经闯过最后一关并处理
                if(player.getLevelNo()==6){
                       int score=LevelParam.levels[player.getLevelNo() - 1].getPerScore() * LevelParam.levels[player.getLevelNo() - 1].getStrTimes();
                       if(player.getCurScore()==score){
                        System.out.println("恭喜您,通关成功!");
                       }else{
                 System.out.println("输入错误,退出!");
                 System.exit(0);
                }

                }

   }
  }else{
   System.out.println("输入错误");
   System.out.println("正确的是"+out);
   System.exit(0);
  }
  
 }

}


Level类



package cn.play1;

public class Level {
 private int LevelNo;                             // 各级别编号
 private int strLength;                           // 各级别一次输出字符串的长度
 private int strTimes;                            // 各级别输出字符串的次数
 private int timeLimit;                           // 各级别闯关的时间限制
 private int perScore;                           // 各级别正确输入一次的得分
 public int getLevelNo() {    
  return LevelNo;
 }
 public void setLevelNo(int levelNo) {
  LevelNo = levelNo;
 }
 public int getStrLength() {
  return strLength;
 }
 public void setStrLength(int strLength) {
  this.strLength = strLength;
 }
 public int getStrTimes() {
  return strTimes;
 }
 public void setStrTimes(int strTimes) {
  this.strTimes = strTimes;
 }
 public int getTimeLimit() {
  return timeLimit;
 }
 public void setTimeLimit(int timeLimit) {
  this.timeLimit = timeLimit;
 }
 public int getPerScore() {
  return perScore;
 }
 public void setPerScore(int perScore) {
  this.perScore = perScore;
 }
 public Level(int levelNo, int strLength, int strTimes, int timeLimit,
   int perScore) {
  super();
  LevelNo = levelNo;
  this.strLength = strLength;
  this.strTimes = strTimes;
  this.timeLimit = timeLimit;
  this.perScore = perScore;
 }
 

}

声明LevelParam  类     创建Level数组



package cn.play1;

public class LevelParam {
定义级别类型数组保存级别信息
 public final static Level levels[]=new Level[6];
// 静态代码块给数组赋初值
 static {
  levels[0]=new Level(1, 2, 10, 30,1);
  levels[1]=new Level(2, 3, 9, 26,2);
  levels[2]=new Level(3, 4, 8, 22,5);
  levels[3]=new Level(4, 5, 7, 18,8);
  levels[4]=new Level(5, 6, 6, 15,10);
  levels[5]=new Level(6, 7, 5, 12,15);
 }

}





Player类




package cn.play1;

import java.util.Scanner;

public class Player {
 private int LevelNo;                                       // 玩家当前级别号
 private int curScore;                                      // 玩家当前积分
 private long startTime=0;                               // 当前玩家级别的开始时间
 private int elapsedTime;                                 // 当前玩家级别的已用时间
 public Player(){
  super();
 }
 public Player(int levelNo, int curScore, long startTime, int elapsedTime) {
  super();
  LevelNo = levelNo;
  this.curScore = curScore;
  this.startTime = startTime;
  this.elapsedTime = elapsedTime;
 }
 public int getLevelNo() {
  return LevelNo;
 }
 public void setLevelNo(int levelNo) {
  LevelNo = levelNo;
 }
 public int getCurScore() {
  return curScore;
 }
 public void setCurScore(int curScore) {
  this.curScore = curScore;
 }
 public long getStartTime() {
  return startTime;
 }
 public void setStartTime(long startTime) {
  this.startTime = startTime;
 }
 public int getElapsedTime() {
  return elapsedTime;
 }
 public void setElapsedTime(int elapsedTime) {
  this.elapsedTime = elapsedTime;
 }
 public void play(){
  Game  game=new Game(this);
  Scanner input=new Scanner(System.in);
  //外层循环每循环一次,级别升一级,积分与计时清零
  for(int i=0;i<LevelParam.levels.length;i++){
   this.LevelNo+=1;
   this.curScore=0;
   this.startTime=System.currentTimeMillis();
   //内层循环输入,输出进行比较
   for(int j=0;j<=LevelParam.levels[LevelNo-1].getStrTimes();j++){
    String out=game.printStr();
    String in=input.next();
    game.printResult(out, in);
     // this.setLevelNo(getLevelNo()+1);
   }
   
  }
  
 }
 
 
 }







  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值