1.12 实例:猜数字小游戏
生活随笔
收集整理的這篇文章主要介紹了
1.12 实例:猜数字小游戏
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
猜數字是一個經典的小游戲,程序先產生一個隨機數,然后用戶輸入數字,程序將輸入的數字與隨機數進行對比,給出用戶相應的提示信息。
本節實現了一個基于 IO 流的猜數字游戲,游戲中限制玩家游戲次數,游戲試玩為 5 次,超過 5 次后,則提示玩家試玩結束,請付費。具體實現步驟和代碼如下:
1)創建 count.txt 文件,存儲游戲次數,文件內容如下:
count=02)創建 way.txt 文件,存儲支付狀態(1 為已付費,0 為未付費),文件內容如下:
way=03)為了簡化代碼,本節將多個實現方法寫在同一個類中。創建 BullCows 類,代碼如下:
public class BullCows {/*** 負責調用對應的方法,實現整個案例的邏輯關系** @param args* @throws IOException*/public static void main(String[] args) throws IOException {while (true) {// 獲取游戲次數int count = getCount();// 獲取付費狀態boolean flag = getCondition();// 如果已付費,提示用戶游戲次數解封可以繼續游戲if (flag) {System.out.println("游戲已經付費,游戲次數已解封!");game();} else {// 未付費且游戲次數超過5次時,提示試玩結束,要付費if (count >= 5) {System.out.println("試玩已經結束,請付費!");getMoney();} else {// 未付費且游戲次數未超過5次時,繼續游戲,游戲次數加1System.out.println("----" + "試玩第" + (count + 1) + "次" + "----");game();writeCount();}}}}/*** 獲取已經玩過的次數** @return temp count.txt文件中的游戲次數* @throws IOException*/private static int getCount() throws IOException {// 創建Properties對象Properties prop = new Properties();// 使用FileReader對象獲取count文件中的游戲次數prop.load(new FileReader("count.txt"));String property = prop.getProperty("count");int temp = Integer.parseInt(property);return temp;}/*** 支付方法,支付成功則把支付狀態改為“1”并存到數據庫,之后可以無限次玩游戲** @throws IOException*/private static void getMoney() throws IOException {System.out.println("請支付5元!");// 獲取鍵盤錄入數據Scanner sc = new Scanner(System.in);int nextInt = sc.nextInt();if (nextInt == 5) {// 創建Properties對象Properties prop = new Properties();prop.setProperty("way", "1");// 使用FileWriter類將支付狀態寫入到way文件prop.store(new FileWriter("way.txt"), null);}}/*** 將試玩的次數寫入文檔并保存** @throws IOException*/private static void writeCount() throws IOException {// 創建Properties對象Properties prop = new Properties();int count = getCount();// 寫入文件prop.setProperty("count", (count + 1) + "");prop.store(new FileWriter("count.txt"), null);}/*** 用來獲取每次啟動時的付費狀態** @return flag 是否付費* @throws FileNotFoundException* @throws IOException*/private static boolean getCondition() throws FileNotFoundException, IOException {boolean flag = false;// 創建Properties對象Properties prop = new Properties();// 讀取way.txt文件,獲取支付狀態prop.load(new FileReader("way.txt"));String property = prop.getProperty("way");int parseInt = Integer.parseInt(property);// way的值等于1時,為已付費if (parseInt == 1) {flag = true;} else {flag = false;}return flag;}/*** 實現游戲產生數字,獲取玩家所猜數字等, 并對玩家每次輸入,都會有相應的提示*/private static void game() {// 產生隨機數1~100int random = (int) (Math.random() * 100 + 1);// 獲取鍵盤錄入數據Scanner sc = new Scanner(System.in);System.out.println("歡迎來到猜數字小游戲!");// while循環進行游戲while (true) {System.out.println("請輸入你猜的數據:");int guess = sc.nextInt();if (guess > random) {System.out.println("大了");} else if (guess < random) {System.out.println("小了");} else {System.out.println("猜對了哦!");break;}}} } 第一次運行時,結果如下: ----試玩第1次---- 歡迎來到猜數字小游戲! 請輸入你猜的數據: 1 小了 請輸入你猜的數據: 5 小了 請輸入你猜的數據: 8 小了 請輸入你猜的數據: 9 小了 請輸入你猜的數據: 10 猜對了哦!此時可以看到 count.txt 文件中 count 的值為 1。當進行 5 次游戲后,運行結果如下: 試玩已經結束,請付費! 請支付5元! 5 游戲已經付費,游戲次數已解封! 歡迎來到猜數字小游戲! 請輸入你猜的數據:此時 count.txt 文件中 count 的值為 5,way.txt 文件中 way 的值為 1。
示例中用到 Properties 類的幾個方法,方法說明如下:
- getProperty (String key):用指定的鍵在此屬性列表中搜索屬性。也就是通過參數 key,得到 key 所對應的
value。 - load (InputStream inStream):從輸入流中讀取屬性列表(鍵和元素對)。通過對指定的文件進行裝載來獲取該文件中的所有鍵值對。以供 getProperty(String key) 來搜索。
- setProperty (String key, String value) :調用 Hashtable 的方法 put,通過調用基類的 put 方法來設置鍵值對。
- store (OutputStream out, String comments):與 load方法相反,該方法是將鍵值對寫入到指定的文件中。
總結
以上是生活随笔為你收集整理的1.12 实例:猜数字小游戏的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 1.11实例:保存图书信息
- 下一篇: 1.1 Java异常(Exception