Java instance guessing numbers games
•
Java
count=0
way=0
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次时,继续游戏,游戏次数加1 System.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~100 int 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; } } } }
----Try for the first time - welcome to the number guessing game! Please enter your guess data: 1 small, please enter your guess data: 5 small, please enter your guess data: 8 small, please enter your guess data: 9 small, please enter your guess data: 10 right!
The trial is over, please pay! Please pay 5 yuan! 5. The game has been paid and the number of games has been unsealed! Welcome to the number guessing game! Please enter the data you guessed:
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码