Java实例之猜数字小游戏

  • 内容
  • 评论
  • 相关

猜数字是一个经典的小游戏,程序先产生一个随机数,然后用户输入数字,程序将输入的数字与随机数进行对比,给出用户相应的提示信息。

本节实现了一个基于 IO 流的猜数字游戏,游戏中限制玩家游戏次数,游戏试玩为 5 次,超过 5 次后,则提示玩家试玩结束,请付费。具体实现步骤和代码如下:

1)创建 count.txt 文件,存储游戏次数,文件内容如下:

count=0

2)创建 way.txt 文件,存储支付状态(1 为已付费,0 为未付费),文件内容如下:

way=0

3)为了简化代码,本节将多个实现方法写在同一个类中。创建 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次时,继续游戏,游戏次数加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;
            }
        }
    }
}

第一次运行时,结果如下:

----试玩第1次----
欢迎来到猜数字小游戏!
请输入你猜的数据:
1
小了
请输入你猜的数据:
5
小了
请输入你猜的数据:
8
小了
请输入你猜的数据:
9
小了
请输入你猜的数据:
10
猜对了哦!

此时可以看到 count.txt 文件中 count 的值为 1。当进行 5 次游戏后,运行结果如下:

试玩已经结束,请付费!
请支付5元!
5
游戏已经付费,游戏次数已解封!
欢迎来到猜数字小游戏!
请输入你猜的数据:

此时 count.txt 文件中 count 的值为 5,way.txt 文件中 way 的值为 1。

示例中用到 Properties 类的几个方法,方法说明如下:

本文标题:Java实例之猜数字小游戏

本文地址:http://www.hosteonscn.com/7675.html

评论

0条评论

发表评论

邮箱地址不会被公开。 必填项已用*标注