首先我们需要定义类,属性,搭建好大致的框架,再依次详细去编写类中的方法 1.Level类 定义用户的不同等级相应的要求
public class Level {
int levelNo;
int strLength;
int strTimes;
int timeLimit;
int 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;
}
}
2.LevelPragram类 定义好不同等级的相应要求
public interface LevelParam {
Level[] LEVELS = {
new Level(1, 2, 2, 20, 3),
new Level(2, 3, 2, 30, 4),
new Level(3, 4, 2, 40, 5),
new Level(4, 5, 2, 50, 3),
new Level(5, 6, 2, 60, 3),
new Level(6, 6, 2, 50, 5),
};
}
3.Player类 初始化属性的起始数据
public class Player {
private String name;
private int lv1No=1;
private long startTime = System.currentTimeMillis();
private long usedTime;
private int totalScores = 0;
public void play(Game g){
g.run(this);
}
public Player(String name){
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getLv1No() {
return lv1No;
}
public void setLv1No(int lv1No) {
this.lv1No = lv1No;
}
public long getStartTime() {
return startTime;
}
public void setStartTime(long startTime) {
this.startTime = startTime;
}
public long getUsedTime() {
return usedTime;
}
public void setUsedTime(long usedTime) {
this.usedTime = usedTime;
}
public int getTotalScores() {
return totalScores;
}
public void setTotalScores(int totalScores) {
this.totalScores = totalScores;
}
}
4.Game类 游戏的主体类 使用了方法的递归
public class Game {
Level[] levels = LevelParam.LEVELS;
Scanner scanner = new Scanner(System.in);
int count;
long tiemSum;
int charCount;
int supportCount;
public void run(Player p){
if(p.getLv1No()>6){
System.out.println("恭喜"+p.getName()+",您已经通关了");
long now = System.currentTimeMillis();
tiemSum = now-p.getStartTime();
System.out.println("总时间"+tiemSum/1000+"s");
System.out.println("一共失误的次数"+count);
System.out.println("一共失误的个数"+charCount);
System.out.println("一共显示的个数"+supportCount);
System.out.println("");
return;
}
int lvno = p.getLv1No();
int cnt = levels[p.getLv1No()-1].strTimes;
int charmNum = levels[p.getLv1No()-1].strLength;
for (int i = 0; i < cnt; i++) {
System.out.print("需要训练的字符串");
String printStr = randomStr(charmNum);
System.out.println(printStr);
boolean isFlag = isCorrect(printStr);
System.out.println(isFlag);
if (!isFlag){
count++;
}
}
++lvno;
if (lvno<=6){
System.out.println("恭喜您升级啦,您现在的等级是"+lvno);
}else {
System.out.println("满级了,离开吧");
}
p.setLv1No(lvno);
run(p);
}
public String randomStr(int charNum){
String str ="";
for (int i = 0; i < charNum; i++) {
int num = (int)(Math.random()*2);
supportCount++;
char c = (char) ((int)(Math.random()*26)+(num==0?65:97));
str += c;
}
return str;
}
public boolean isCorrect(String str){
System.out.print("请开始输入");
String input = scanner.next();
char[] c1 = input.toCharArray();
char[] c2 = str.toCharArray();
for (int i = 0; i < c2.length ; i++) {
if (c1[i]!= c2[i]){
charCount++;
}
}
if (str.equals(input)){
return true;
}
return false;
}
}
6.Test 测试类
public class Test {
public static void main(String[] args) {
Player player = new Player("张三");
player.play(new Game());
}
}
最后实现效果 其中不足之处:虽然输入错误,我也进行积分的相加,然后进入下一关!
|