成人视屏在线观看-国产99精品-国产精品1区2区-欧美一级在线观看-国产一区二区日韩-色九九九

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

java類與對(duì)象案例之打字游戲

瀏覽:23日期:2022-08-29 15:34:30

類與對(duì)象案例-童年回憶之打字游戲

一、玩家類二、等級(jí)類三、游戲類四、等級(jí)地圖五、測(cè)試類

這次要做的案例是一個(gè)打字游戲的案例,相信大家小時(shí)候都玩過金山打字通的警察抓小偷和飛機(jī)大戰(zhàn),這次的案例是類似的簡(jiǎn)易版。

首先對(duì)于這個(gè)案例,我們要解決的是如何生成隨機(jī)的字符串,如何判斷生成的字符串和輸入的字符串是否相等。

一、玩家類

package com.yc.oop6.hc0705;public class Player { private int score; //積分 private long startTime; //各級(jí)別的開始時(shí)間 private long dis; //每次闖關(guān)剩余時(shí)間 private int levelNo; //級(jí)別號(hào)碼 public int getScore() { return score; } public void setScore(int score) { this.score = score; } public long getStartTime() { return startTime; } public void setStartTime(long startTime) { this.startTime = startTime; } public int getLevelNo() { return levelNo; } public void setLevelNo(int levelNo) { this.levelNo = levelNo; } public Player(int score, long startTime, int levelNo) { super(); this.score = score; this.startTime = startTime; this.levelNo = levelNo; } public Player() { super(); } public long getDis() { return dis; } public void setDis(long dis) { this.dis = dis; } }

二、等級(jí)類

package com.yc.oop6.hc0705;public class Level { private int levelNo; //第幾關(guān) private int strLength; //字符串長(zhǎng)度 private int strTime; //需要輸入的次數(shù) private int timeLimit; //時(shí)間限制 private int score; //答對(duì)一次獲得的積分 public int getLevelNo() { return levelNo; } public int getStrLength() { return strLength; } public int getStrTime() { return strTime; } public int getTimeLimit() { return timeLimit; } public int getScore() { return score; } public Level(int levelNo, int strLength, int strTime, int timeLimit, int score) { super(); this.levelNo = levelNo; this.strLength = strLength; this.strTime = strTime; this.timeLimit = timeLimit; this.score = score; } public Level() { super(); } }

三、游戲類

package com.yc.oop6.hc0705;import java.util.Random;import java.util.Scanner;public class Game { private Player player; private Random r = new Random(); private Scanner sc = new Scanner(System.in) ; public Game(Player player) { this.player = player; } //開始游戲 public void startGame() { System.out.println('游戲開始'); //開始闖關(guān) for(int i = 0; i < Levels.ls.length; i++) { System.out.println('開始進(jìn)入第' + (i+1) + '關(guān)'); //每一關(guān)的開始在這里 player.setLevelNo(player.getLevelNo() + 1); player.setStartTime(System.currentTimeMillis()); //循環(huán),這一關(guān)要輸入多少次 for(int j = 0 ; j < Levels.ls[player.getLevelNo() - 1].getStrTime() ; j++ ) { String out = printStr(); System.out.println(out); String in = sc.nextLine(); boolean flag = result(in, out); if(flag == false) { System.exit(1);; } } //每剩下1s 增加20分 player.setScore(player.getScore() + (int)player.getDis() * 20); System.out.println('完成第' + (i+1) + '關(guān),您現(xiàn)在的積分為:' + player.getScore()); System.out.println('進(jìn)入下一關(guān)'); } } //隨機(jī)字符串 public String printStr( ) { String str = ''; double rNum = 0; for(int i = 0; i < Levels.ls[player.getLevelNo()-1].getStrLength(); i++ ) { rNum = r.nextDouble(); if(rNum < 0.3333) { str += (r.nextInt(9)+1); }else if(rNum < 0.6667){ str += (char)(r.nextInt(26)+65); }else { str += (char)(r.nextInt(26)+97); } } return str; } //字符串對(duì)比 public boolean result(String in, String out) { //字符串判斷 if(out.equals(in)) { //先獲取一下當(dāng)前的系統(tǒng)時(shí)間 long endTimes = System.currentTimeMillis(); long startTimes = player.getStartTime(); //獲得下一關(guān)的時(shí)間 long dis = Levels.ls[player.getLevelNo() -1 ].getTimeLimit(); if((endTimes - startTimes)/1000 > dis) { System.out.println('游戲超時(shí),游戲結(jié)束,您的最終得分為:' + player.getScore()); return false; }else { //輸入正確,且沒有超時(shí) //加積分 int score = Levels.ls[player.getLevelNo()].getScore(); player.setDis(dis - (endTimes - startTimes)/1000); player.setScore(player.getScore() + score ); System.out.println('輸入正確,您現(xiàn)在的積分為:'+ player.getScore() +' ,這一關(guān)您還有: ' + player.getDis() + ' 秒鐘'); return true; } }else { System.out.println('輸入錯(cuò)誤,游戲結(jié)束,您的最終得分為:' + player.getScore()); return false;//輸入錯(cuò)誤 } }}

四、等級(jí)地圖

package com.yc.oop6.hc0705;public class Levels { //定義一個(gè)靜態(tài)的對(duì)象數(shù)組 public static Level ls[] = new Level[7]; static { ls[0] = new Level(1,2,5,20,10); ls[1] = new Level(2,3,5,18,20); ls[2] = new Level(3,4,4,16,30); ls[3] = new Level(4,5,4,15,40); ls[4] = new Level(5,6,4,15,50); ls[5] = new Level(6,7,3,15,60); ls[6] = new Level(7,8,3,15,70); }}

五、測(cè)試類

package com.yc.oop6.hc0705;public class Test { public static void main(String[] args) { Player p = new Player(); Game g = new Game(p); g.startGame(); }}

詳細(xì)的解釋都在代碼的注釋里了,大家細(xì)品。

更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,分享給大家:

C++經(jīng)典小游戲匯總

python經(jīng)典小游戲匯總

python俄羅斯方塊游戲集合

JavaScript經(jīng)典游戲 玩不停

java經(jīng)典小游戲匯總

javascript經(jīng)典小游戲匯總

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 精品久久精品久久 | 欧美一级视频在线 | 久久精品爱 | 久久视频精品36线视频在线观看 | 久久精品视频2 | 国产成人午夜精品5599 | 污到下面流水的视频 | 成人免费一区二区三区在线观看 | 欧美视频一二三区 | 996久久国产精品线观看 | tom影院亚洲国产日本一区 | 亚洲国产成人久久午夜 | 色综合久久加勒比高清88 | 一区二区三区免费视频 www | 特级深夜a级毛片免费观看 特级生活片 | 久久精品视频6 | 一个人免费观看日本www视频 | 亚洲一区二区三区高清 | 一级毛片视频 | 日韩一级欧美一级毛片在 | 久久久久欧美精品观看 | 亚洲一区二区久久 | 国产精品毛片久久久久久久 | 美女黄色毛片免费看 | 91香蕉视| 一区二区影院 | 中文偷拍视频在线观看 | 欧美精品国产制服第一页 | 中文字幕日韩精品有码视频 | 久污| 国产一区二区精品久 | 亚洲韩精品欧美一区二区三区 | 毛片在线视频 | 国产一区二区亚洲精品 | 亚洲天堂视频在线观看 | 特级做人爱c级特级aav毛片 | 在线亚洲精品自拍 | a级国产乱理伦片在线观看 a级国产乱理伦片在线观看99 | 欧美成人免费xxx大片 | 亚洲激情 欧美 | 国内交换一区二区三区 |