欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java類與對象案例之打字游戲

 更新時間:2020年07月07日 08:40:32   作者:程序少年籃球king  
這篇文章主要為大家詳細(xì)介紹了java類與對象案例之打字游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

一、玩家類
二、等級類
三、游戲類
四、等級地圖
五、測試類

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

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

一、玩家類

package com.yc.oop6.hc0705;

public class Player {
 private int score; //積分
 private long startTime; //各級別的開始時間
 private long dis;   //每次闖關(guān)剩余時間
 private int levelNo; //級別號碼
 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;
 }
 
 
}

二、等級類

package com.yc.oop6.hc0705;

public class Level {
 private int levelNo; //第幾關(guān)
 private int strLength; //字符串長度
 private int strTime; //需要輸入的次數(shù)
 private int timeLimit; //時間限制
 private int score; //答對一次獲得的積分
 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;
 }
 
 //字符串對比
 
 public boolean result(String in, String out) {
 //字符串判斷
 if(out.equals(in)) {
 //先獲取一下當(dāng)前的系統(tǒng)時間
 long endTimes = System.currentTimeMillis();
 long startTimes = player.getStartTime();
 //獲得下一關(guān)的時間
 long dis = Levels.ls[player.getLevelNo() -1 ].getTimeLimit();
 
 if((endTimes - startTimes)/1000 > dis) {
 System.out.println("游戲超時,游戲結(jié)束,您的最終得分為:" + player.getScore());
 return false;
 }else {
 //輸入正確,且沒有超時
 //加積分
 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("輸入錯誤,游戲結(jié)束,您的最終得分為:" + player.getScore());
 return false;//輸入錯誤
 }
 
 
 }
}

四、等級地圖

package com.yc.oop6.hc0705;

public class Levels {
 //定義一個靜態(tài)的對象數(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);
 }
}

五、測試類

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)典小游戲匯總

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Ribbon負(fù)載均衡服務(wù)調(diào)用的示例詳解

    Ribbon負(fù)載均衡服務(wù)調(diào)用的示例詳解

    Rbbo其實(shí)就是一個軟負(fù)載均衡的客戶端組件,他可以和其他所需請求的客戶端結(jié)合使用,這篇文章主要介紹了Ribbon負(fù)載均衡服務(wù)調(diào)用案例代碼,需要的朋友可以參考下
    2023-01-01
  • 在實(shí)踐中了解Java反射機(jī)制應(yīng)用

    在實(shí)踐中了解Java反射機(jī)制應(yīng)用

    當(dāng)程序運(yùn)行時,允許改變程序結(jié)構(gòu)或變量類型,這種語言稱為動態(tài)語言。我們認(rèn)為java并不是動態(tài)語言,但是它卻有一個非常突出的動態(tài)相關(guān)機(jī)制,俗稱:反射。下面我們來簡單學(xué)習(xí)一下吧
    2019-05-05
  • 詳解Java分布式事務(wù)的 6 種解決方案

    詳解Java分布式事務(wù)的 6 種解決方案

    在分布式系統(tǒng)、微服務(wù)架構(gòu)大行其道的今天,服務(wù)間互相調(diào)用出現(xiàn)失敗已經(jīng)成為常態(tài),本文側(cè)重于其他幾項(xiàng),關(guān)于 2PC、3PC 傳統(tǒng)事務(wù),網(wǎng)上資料已經(jīng)非常多了,這里不多做重復(fù),本文通過示例給大家介紹Java分布式事務(wù)的 6 種解決方案,一起看看吧
    2021-06-06
  • Spring Cloud Alibaba和Dubbo融合實(shí)現(xiàn)

    Spring Cloud Alibaba和Dubbo融合實(shí)現(xiàn)

    這篇文章主要介紹了Spring Cloud Alibaba和Dubbo融合實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-04-04
  • SpringBoot2零基礎(chǔ)到精通之映射與常用注解請求處理

    SpringBoot2零基礎(chǔ)到精通之映射與常用注解請求處理

    SpringBoot是一種整合Spring技術(shù)棧的方式(或者說是框架),同時也是簡化Spring的一種快速開發(fā)的腳手架,本篇讓我們一起學(xué)習(xí)映射、常用注解和方法參數(shù)的小技巧
    2022-03-03
  • SpringBoot在一定時間內(nèi)限制接口請求次數(shù)的實(shí)現(xiàn)示例

    SpringBoot在一定時間內(nèi)限制接口請求次數(shù)的實(shí)現(xiàn)示例

    在項(xiàng)目中,接口的暴露在外面,很多人就會惡意多次快速請求,本文主要介紹了SpringBoot在一定時間內(nèi)限制接口請求次數(shù)的實(shí)現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下
    2022-03-03
  • Idea servlet映射方法優(yōu)缺點(diǎn)對比

    Idea servlet映射方法優(yōu)缺點(diǎn)對比

    這篇文章主要介紹了Idea servlet映射方法優(yōu)缺點(diǎn)對比,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-11-11
  • easycode配置成mybatis-plus模板的實(shí)現(xiàn)方法

    easycode配置成mybatis-plus模板的實(shí)現(xiàn)方法

    本文主要介紹了easycode配置成mybatis-plus模板的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • SpringBoot實(shí)現(xiàn)多端口監(jiān)聽的代碼示例

    SpringBoot實(shí)現(xiàn)多端口監(jiān)聽的代碼示例

    當(dāng)你需要在同一個Spring Boot應(yīng)用中,通過不同的端口來提供不同的服務(wù)或功能時,就需要實(shí)現(xiàn)多端口監(jiān)聽,所以本文給大家介紹了SpringBoot實(shí)現(xiàn)多端口監(jiān)聽的方法示例,并有相關(guān)的代碼示例供大家參考,需要的朋友可以參考下
    2024-09-09
  • Springboot 中使用 Aop代碼實(shí)戰(zhàn)教程

    Springboot 中使用 Aop代碼實(shí)戰(zhàn)教程

    AOP的編程思想是把對類對象的橫切問題點(diǎn),從業(yè)務(wù)邏輯中分離出來,從而達(dá)到解耦的目的,增加代碼的復(fù)用性,提高開發(fā)效率,這篇文章主要介紹了Springboot中使用Aop代碼實(shí)戰(zhàn)教程,需要的朋友可以參考下
    2023-07-07

最新評論