在这里插package quickHit;
public class Game {
private Player player;
public Game() {
super();
// TODO Auto-generated constructor stub
}
public Game(Player player) {
super();
this.player = player;
}
public Player getPlayer() {
return player;
}
public void setPlayer(Player player) {
this.player = player;
}
//根据当前级别获取字符串的方法
public String Printstr() {
int strlength = LevelStandard.arr[this.player.getLevel()-1].getStrLength();
String[] strs= {"1","2","3"};
String str="";
for (int i = 0; i < strlength; i++) {
//产生一个随机数
int index=(int)(Math.random()*3);
str+=strs[index];
}
return str;
}
//根据产生的字符串和用户输入的字符串比较是否超时
public boolean Judge(String str,String str1) {
return str.equals(str1);
}
}
入代码片
在package quickHit;
public class Level {
private int levelNum;
private int strLength;
private int strCount;
private int time;
private int score;
public Level() {
super();
// TODO Auto-generated constructor stub
}
public Level(int levelNum, int strLength, int strCount, int time, int score) {
super();
this.levelNum = levelNum;
this.strLength = strLength;
this.strCount = strCount;
this.time = time;
this.score = score;
}
public int getLevelNum() {
return levelNum;
}
public void setLevelNum(int levelNum) {
this.levelNum = levelNum;
}
public int getStrLength() {
return strLength;
}
public void setStrLength(int strLength) {
this.strLength = strLength;
}
public int getStrCount() {
return strCount;
}
public void setStrCount(int strCount) {
this.strCount = strCount;
}
public int getTime() {
return time;
}
public void setTime(int time) {
this.time = time;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
这里插入代码片
在这package quickHit;
import java.util.Scanner;
public class Player {
private int time;
private int score;
private int level=1;
public Player() {
super();
// TODO Auto-generated constructor stub
}
public Player(int time, int score, int level) {
super();
this.time = time;
this.score = score;
this.level = level;
}
public int getTime() {
return time;
}
public void setTime(int time) {
this.time = time;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public void play() {
Scanner input=new Scanner(System.in);
Game game = new Game(this);
Level level = LevelStandard.arr[this.level - 1];
System.out.println("开始游戏");
for (int i = this.level - 1; i < LevelStandard.arr.length; i++) {
//获取当前系统时间
long startcurrentTimeMillis = System.currentTimeMillis();
this.time=0;
this.score=0;
// 根据当前级别决定产生多少次字符串
for (int j = 0; j < LevelStandard.arr[this.level - 1].getStrCount(); j++) {
// 游戏生成字符串
String str = game.Printstr();
System.out.println(str);
//输入字符串
String inputStr=input.next();
//判断你输入的字符串和产生的字符串是否一致
boolean flag=game.Judge(str,inputStr);
if(flag) {
//输入一致 在判断时间
//获取当前系统时间
long endcurrentTimeMillis = System.currentTimeMillis();
int usetime=(int)((endcurrentTimeMillis-startcurrentTimeMillis)/1000);
this.time+=usetime;
if(this.time<LevelStandard.arr[this.level - 1].getTime()) {
//在规定时间正确输入了字符
//积分累积
this.score+=LevelStandard.arr[this.level - 1].getScore();
System.out.println("当前级别是"+this.level+"积分是"+this.score+"用时是"+this.time);
}else {
//超时了
System.out.println("超时退出");
return;
}
}else {
//输入不一致
System.out.println("游戏结束 输入错误");
return;
}
}
//升级
this.level+=1;
System.out.println("升级了");
}
System.out.println("恭喜你闯关成功");
}
}
里插入代码片
package quickHit;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Player play = new Player();
play.play();
}
}
在这里插package quickHit;public class Game { private Player player; public Game() { super(); // TODO Auto-generated constructor stub } public Game(Player player) { super(); this.player = player; } public Player getPlayer() { return play