在這裡插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;
public class LevelStandard {
static Level arr[] = new Level[6];
static {
//等級 字元長度 字元次數 時間 得分
arr[0]=new Level(1,1,6,30,1);
arr[1]=new Level(2,2,5,30,2);
arr[2]=new Level(3,3,4,30,3);
arr[3]=new Level(4,4,3,30,4);
arr[4]=new Level(5,5,2,30,5);
arr[5]=new Level(6,6,2,30,6);
}
}
裡插入代碼片
在這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();
}
}
在這開始遊戲
2
2
目前級别是1積分是1用時是1
2
2
目前級别是1積分是2用時是3
1
1
目前級别是1積分是3用時是6
2
2
目前級别是1積分是4用時是10
3
3
目前級别是1積分是5用時是16
2
2
目前級别是1積分是6用時是22
更新了
12
12
目前級别是2積分是2用時是1
22
22
目前級别是2積分是4用時是3
33
33
目前級别是2積分是6用時是6
31
31
目前級别是2積分是8用時是11
21
21
目前級别是2積分是10用時是17
更新了
232
232
目前級别是3積分是3用時是1
332
332
目前級别是3積分是6用時是4
233
233
目前級别是3積分是9用時是8
322
322
目前級别是3積分是12用時是16
更新了
3121
3121
目前級别是4積分是4用時是2
2312
2312
目前級别是4積分是8用時是6
2222
2222
目前級别是4積分是12用時是12
更新了
11111
11111
目前級别是5積分是5用時是2
12132
12132
目前級别是5積分是10用時是6
更新了
133313
133313
目前級别是6積分是6用時是4
121212
121212
目前級别是6積分是12用時是10
更新了
恭喜你闖關成功
裡插入代碼片