QuickHit案例

import java.util.Scanner;

/**
* 玩家类
* @author Administrator
*
*/

public class Player {
private int levelNo;//玩家当前级别号
private int curScore;//玩家当前积分
private long startTime;//当前级别开始时间
private int elapsedTime;//当前级别已用时间
//带参构造
public Player(int levelNo, int curScore, long startTime, int elapsedTime) {
this.levelNo = levelNo;
this.curScore = curScore;
this.startTime = startTime;
this.elapsedTime = elapsedTime;
}
//无参构造
public Player() {

}
/**
* 封装 快捷键(alt+shift+s r alt+a 点击OK)
* @return
*/
public int getLevelNo() {
return levelNo;
}
public void setLevelNo(int levelNo) {
this.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的带参.this代表Player属性
Game game=new Game(this);
Scanner input=new Scanner(System.in);
for (int i = 0; i <LevelParam.level.length; i++) {
//晋级
this.levelNo+=1;
//晋级后计时清零
this.startTime=System.currentTimeMillis();
this.curScore=0;
//满级通关
if(levelNo==6){
System.out.println("通关了");
break;
}
//内层循环,输出输入比较
for (int j = 0; j < LevelParam.level[levelNo-1].getStrTimes(); j++) {
//输出随机产生的字符串
String outStr=game.printStr();
//接收用户输入的字符串
String inStr=input.next();
//调用game的printResult方法,对比
game.printResult(outStr, inStr);
}

}

}

}

import java.util.Random;

/**
* 游戏类
* @author Administrator
*
*/

public class Game {
public Player player;
/**
* 封装(生成私有字段的公有get和set)
* @return
*/
public Player getPlayer() {
return player;
}
public void setPlayer(Player player) {
this.player = player;
}
/**
* 带参方法
* @param player
*/
public Game(Player player) {
this.player = player;
}
/**
* 无参方法
*/
public Game(){

}
public String printStr(){
/**
* 定义一个int类型的对应各级编号应输出字符串的长度
*/
int strLength=LevelParam.level[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;
}
}
//输出
String str=buffer.toString();
System.out.println(str);
return str;
}

public void printResult(String out,String in){
long currentTime=System.currentTimeMillis();
//判断随机产生的与输入的是否一致
if(out.equals(in)){
//判断是否超时
if((currentTime-player.getStartTime())/1000>LevelParam.level[player.getLevelNo()-1].getTimeLimit()){
System.out.println("菜鸟输入速度太慢了吧!");
/**
* System.exit(1);表示异常退出, System.exit(0);表示正常退出
*/
System.exit(1);
}else{
//计算当前积分
player.setCurScore(player.getCurScore()+LevelParam.level[player.getLevelNo()-1].getPerScore());
//计算时间
player.setElapsedTime((int)(currentTime-player.getStartTime())/1000);
//输出级别,积分和时间
System.out.println("输入正确,您的级别"+player.getLevelNo()+"积分"+player.getCurScore()+"已用时间"+player.getElapsedTime()+"秒<");
}
}else{
System.out.println("菜鸟输入错误了");
/**
* System.exit(1);表示异常退出, System.exit(0);表示正常退出
*/
System.exit(1);

}
}

}

/**
* 级别类
* @author Administrator
*
*/
public class Level {
public int levelNo; //各级编号
public int strLength;//各级别一次输入字符串的长度
public int strTimes;//各级别输入字符串的次数
public int timeLimit;//各级别闯关的时间限制
public int perScore;//各级别正确输入一次的得分

public int getLevelNo() {
return levelNo;
}
public void setLevelNo(int levelNo) {
this.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(){

}
/**
* 带参方法
* @param levelNo
* @param strLength
* @param strTimes
* @param timeLimit
* @param perScore
*/
public Level(int levelNo, int strLength, int strTimes, int timeLimit,int perScore) {
this.levelNo = levelNo;
this.strLength = strLength;
this.strTimes = strTimes;
this.timeLimit = timeLimit;
this.perScore = perScore;
}
}

public class LevelParam {
public final static Level level[]=new Level[6];
/**
* 静态代码块,给数组赋值
*/
static{
level[0]=new Level(1,2,10,30,1);
level[1]=new Level(2,3,9,26,2);
level[2]=new Level(3,4,8,22,5);
level[3]=new Level(4,5,7,18,8);
level[4]=new Level(5,6,6,15,10);
level[5]=new Level(6,7,2,12,15);
}

}

/**
* 测试类
* @author Administrator
*
*/
public class Test {
public static void main(String[] args) {
Player player=new Player();
player.play();
}

}

原文地址:https://www.cnblogs.com/sujulin/p/6653187.html