QuickHit 项目

时间:2023-03-09 23:11:41
QuickHit 项目

QuickHit 项目

QuickHit 项目

package cn.javaoppday01;

import java.util.Random;

public class Game {
public Player player; public Game(Player player) {
this.player = player;
}
public void printResult(String out, String in) { long currentTime = System.currentTimeMillis(); if ((currentTime - player.getStartTime()) / > LevelParam.levels[player
.getLevelNo() - ].getTimeLimit()) {
System.out.println("你输入太慢了,已经超时,退出!");
System.exit();
} else if (!out.equals(in)) {
System.out.println("你输入错了了退出!");
System.exit();
} // 计算玩家当前积分
player.setCurScore(player.getCurScore()
+ LevelParam.levels[player.getLevelNo() - ].getPerScore());
System.out.println("下一关" + "您的当前积分是" + player.getCurScore()); } // 输出字符串,返回字符串用于和玩家输入比较。
public String printStr() {
StringBuffer buffer = new StringBuffer();
Random random = new Random();
// 通过循环生成要输出的字符串
for (int i = ; i < ; i++) {
int rand = random.nextInt(); // 产生随机数
// 根据随机数拼接字符串
switch (rand) {
case :
buffer.append(">");
break;
case :
buffer.append("<");
break;
case :
buffer.append("a");
break;
case :
buffer.append("b");
break;
case :
buffer.append("c");
break;
case :
buffer.append("d");
break;
case :
buffer.append("e");
break;
}
} return buffer.toString();
} }

游戏类

package cn.javaoppday01;

public class Level {

    private int levelNo;//级别号
private int strLength;//各级别一次输出字符串的长度
private int stTtime;//各级别输出字符串次数
private int timeLimit;//各级别闯关限时的时间
private 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 getStTtime() {
return stTtime;
}
public void setStTtime(int stTtime) {
this.stTtime = stTtime;
}
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 stTtime ,int timeLimit,int perScore){
this.levelNo=levelNo;
this.strLength=strLength;
this.stTtime=stTtime;
this.timeLimit=timeLimit;
this.perScore=perScore; }
}

等级类

package cn.javaoppday01;

import java.util.Scanner;

public class Player {

    private int levelNo;//级别号
private int curScore;//当前积分
private long startTime;//各级别开始时间
private int elapsedTime;//各级别已用时间
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 game =new Game(this);
Scanner input =new Scanner(System.in);
for(int i=;i<LevelParam.levels.length;i++){
this.levelNo+=;
this.startTime=System.currentTimeMillis();
this.curScore=;
for(int j=;j<LevelParam.levels[levelNo-].getStTtime();j++){
String outStr =game.printStr();
System.out.println(outStr);
String inStr =input.next();
game.printResult(outStr,inStr); } }
} }

玩家类

package cn.javaoppday01;

public class LevelParam {

    public final static Level levels[] =new Level[];//对应六个级别
static{
levels[] =new Level(,,,,);
levels[] =new Level(,,,,);
levels[] =new Level(,,,,);
levels[] =new Level(,,,,);
levels[] =new Level(,,,,);
levels[] =new Level(,,,,); }
}

添加类

package cn.javaoppday01;

public class MyMain {
public static void main(String[] args) {
Player play=new Player();
play.play();
}
}

Main方法测试类