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