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("輸入正確,您的級別"+player.getLevelNo()+",您的積分"+player.getCurScore()+",已用時(shí)"+player.getElapsedTime()+"秒。");
}
}else{
System.out.println("輸入錯(cuò)誤,退出!");
System.exit(1);
}
}
}
public class Level {
private int levelNo;//各級別編號
private int strLength;//各級別一次輸出字符串的長度
private int strTimes;//各級別輸出字符串的次數(shù)
private int timeLimit;//各級別闖關(guān)的時(shí)間限制
private int perScore;//各級別正確輸入一次的得分
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);//編號、字符串長度、字符串次數(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)前級別
private int curScore;//當(dāng)前級別積分
private long startTime;//當(dāng)前級別開始時(shí)間
private long elapsedTime;//當(dāng)前級別已用時(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)專題,分享給大家:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決feign接口返回泛型設(shè)置屬性為null的問題
這篇文章主要介紹了解決feign接口返回泛型設(shè)置屬性為null的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
java使用RandomAccessFile類基于指針讀寫文件實(shí)例代碼
這篇文章主要介紹了java使用RandomAccessFile類基于指針讀寫文件實(shí)例代碼,具有一定參考價(jià)值,需要的朋友可以了解下。2017-10-10
2020新版idea創(chuàng)建項(xiàng)目沒有javaEE 沒有Web選項(xiàng)的完美解決方法
這篇文章主要介紹了2020新版idea創(chuàng)建項(xiàng)目沒有javaEE 沒有Web選項(xiàng)的完美解決方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
記錄jdk21連接SQLServer因?yàn)門LS協(xié)議報(bào)錯(cuò)問題
在使用Druid連接池連接SQL Server時(shí),可能會遇到因TLS版本不匹配導(dǎo)致的連接失敗問題,具體表現(xiàn)為客戶端使用TLS1.3或TLS1.2,而SQL Server僅支持TLS1.0,導(dǎo)致無法建立安全連接,解決方法是修改JDK的安全配置,啟用TLS1.02024-10-10
使用ClassFinal實(shí)現(xiàn)SpringBoot項(xiàng)目jar包加密的操作指南
在實(shí)際開發(fā)中,保護(hù)項(xiàng)目的安全性和保密性是至關(guān)重要的,針對于 Spring Boot 項(xiàng)目,我們需要將 JAR 包進(jìn)行加密從而有效地防止未經(jīng)授權(quán)的訪問和修改,本文將介紹如何使用ClassFinal在 Spring Boot 項(xiàng)目中實(shí)現(xiàn) JAR 包加密,需要的朋友可以參考下2024-06-06
SpringBoot?@Transactional事務(wù)不生效排查方式
這篇文章主要介紹了SpringBoot?@Transactional事務(wù)不生效排查方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
Java深入講解instanceof關(guān)鍵字的使用
instanceof 是 Java 的一個(gè)二元操作符,類似于 ==,>,< 等操作符。instanceof 是 Java 的保留關(guān)鍵字。它的作用是測試它左邊的對象是否是它右邊的類的實(shí)例,返回 boolean 的數(shù)據(jù)類型2022-05-05

