java實(shí)現(xiàn)快速打字游戲
本文實(shí)例為大家分享了java實(shí)現(xiàn)打字游戲的具體代碼,供大家參考,具體內(nèi)容如下
import java.util.Random;
import java.util.Scanner;
public class Game {
public Game(Player player) {
}
public Game() {
}
public String printStr(Player player) {
StringBuffer a=new StringBuffer();
for(int i=0;i<LevelParam.levels[player.getLevelNo()-1].getStrLength();i++) {
int num=(int)(Math.random()*(7));
switch(num) {
case 1:
a.append(">");
break;
case 2:
a.append("<");
break;
case 3:
a.append("+");
break;
case 4:
a.append("-");
break;
case 5:
a.append("*");
break;
case 6:
a.append("/");
break;
case 0:
a.append("=");
break;
}
}
String str=a.toString();
System.out.println(str);
return str;
}
public void printResult(Player player) {
String num1=this.printStr(player);
Scanner input=new Scanner(System.in);
player.setStartTime();
String num2=input.next();
long currentTime=System.currentTimeMillis();
player.setElapsedTime(currentTime,player.getStartTime());
if (num1.equals(num2)) {
if ((currentTime-player.getStartTime())/1000>LevelParam.levels[player.getLevelNo()-1].getTimeLimit()) {
System.out.println("你輸入太慢了,輸入超時(shí),退出。");
System.exit(1);
} else {
player.setCurScore(player);
System.out.println("輸入正確,您的級(jí)別"+player.getLevelNo()+",您的積分"+player.getCurScore()+",已用時(shí)"+player.getElapsedTime()+"秒。");
}
}else{
System.out.println("輸入錯(cuò)誤,退出!");
System.exit(1);
}
}
}
public class Level {
private int levelNo;//各級(jí)別編號(hào)
private int strLength;//各級(jí)別一次輸出字符串的長(zhǎng)度
private int strTimes;//各級(jí)別輸出字符串的次數(shù)
private int timeLimit;//各級(jí)別闖關(guān)的時(shí)間限制
private int perScore;//各級(jí)別正確輸入一次的得分
public int getLevelNo() {
return levelNo;
}
public int getStrLength() {
return strLength;
}
public int getStrTimes() {
return strTimes;
}
public int getTimeLimit() {
return timeLimit;
}
public int getPerScore() {
return 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 static final Level levels[]=new Level[6];
static{
levels[0]=new Level(1,2,10,30,1);//編號(hào)、字符串長(zhǎng)度、字符串次數(shù)、關(guān)卡時(shí)間、正確得分
levels[1]=new Level(2,3,9,26,2);
levels[2]=new Level(3,4,8,22,5);
levels[3]=new Level(4,5,7,18,8);
levels[4]=new Level(5,6,6,15,10);
levels[5]=new Level(6,7,5,12,15);
}
}
import java.util.Scanner;
public class Player {
private int levelNo=1;//玩家當(dāng)前級(jí)別
private int curScore;//當(dāng)前級(jí)別積分
private long startTime;//當(dāng)前級(jí)別開(kāi)始時(shí)間
private long elapsedTime;//當(dāng)前級(jí)別已用時(shí)間
public int getLevelNo() {
return levelNo;
}
public void setLevelNo() {
this.levelNo++;
}
public int getCurScore() {
return curScore;
}
public void setCurScore(Player player) {
this.curScore = curScore+LevelParam.levels[player.getLevelNo()-1].getPerScore();;
}
public void setCurScore(int a) {
this.curScore=a;
}
public long getStartTime() {
return startTime;
}
public void setStartTime() {
this.startTime =System.currentTimeMillis();;
}
public long getElapsedTime() {
return elapsedTime;
}
public void setElapsedTime(long a,long b) {
this.elapsedTime=(a-b)/1000+elapsedTime;
}
public void setElapsedTime(int a) {
this.elapsedTime=a;
}
public void play(Player player) {
Game one=new Game();
for(int j=0;j<6;j++) {
for(int i=0;i<LevelParam.levels[player.getLevelNo()-1].getStrTimes();i++) {
one.printResult(player);
}
player.setLevelNo();
player.setCurScore(0);
player.setElapsedTime(0);
}
}
}
public class Test {
public static void main(String[] args) {
Player p=new Player();
p.play(p);
}
}
更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,分享給大家:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springboot詳解實(shí)現(xiàn)車險(xiǎn)理賠信息管理系統(tǒng)代碼
本系統(tǒng)基于Springboot開(kāi)發(fā)實(shí)現(xiàn)了一個(gè)為用戶車險(xiǎn)進(jìn)行理賠信息管理的一個(gè)信息化管理系統(tǒng),核心的業(yè)務(wù)主要是用戶申請(qǐng)保險(xiǎn)理賠,管理員審核進(jìn)入理賠程序,事故調(diào)查員對(duì)事故進(jìn)行調(diào)查和現(xiàn)場(chǎng)勘察,這其中共涉及到三類用戶,購(gòu)買(mǎi)保險(xiǎn)的客戶,事故調(diào)查員和系統(tǒng)管理員2022-06-06
java實(shí)現(xiàn)word文檔轉(zhuǎn)pdf并添加水印的方法詳解
這篇文章主要介紹了java實(shí)現(xiàn)word文檔轉(zhuǎn)pdf并添加水印的方法,結(jié)合實(shí)例形式詳細(xì)分析了java word文檔轉(zhuǎn)PDF相關(guān)實(shí)現(xiàn)技巧與操作注意事項(xiàng),需要的朋友可以參考下2019-09-09
SWT(JFace) 圖片瀏覽器 實(shí)現(xiàn)代碼
SWT(JFace)小制作:圖片瀏覽器2009-06-06
JAVA Spring Boot 自動(dòng)配置實(shí)現(xiàn)原理詳解
這篇文章主要介紹了詳解SpringBoot自動(dòng)配置原理,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2021-09-09
Java數(shù)據(jù)結(jié)構(gòu)之二叉查找樹(shù)的實(shí)現(xiàn)
二叉查找樹(shù)(亦稱二叉搜索樹(shù)、二叉排序樹(shù))是一棵二叉樹(shù),且各結(jié)點(diǎn)關(guān)鍵詞互異,其中根序列按其關(guān)鍵詞遞增排列。本文將通過(guò)示例詳細(xì)講解二叉查找樹(shù),感興趣的可以了解一下2022-03-03
關(guān)于SaCheckPermission權(quán)限校驗(yàn)注解
在若依框架(RuoYi)的前后端分離版4.8.x中,SaCheckPermission注解用于權(quán)限校驗(yàn),這個(gè)注解可以應(yīng)用在方法上,以確保只有具有相應(yīng)權(quán)限的用戶才能訪問(wèn)該方法2024-11-11

