用java实现24点游戏

前言

用java实现24点游戏
Welocome to Xiang’s world~
在这里插入图片描述

题目ღ( ´・ᴗ・` )

24点游戏是经典的纸牌益智游戏。
常见游戏规则:
从扑克中每次取出4张牌。使用加减乘除,第一个能得出24者为赢。(其中,J代表11,Q代表12,K代表13,A代表1),按照要求编程解决24点游戏。
基本要求: 随机生成4个代表扑克牌牌面的数字字母,程序自动列出所有可能算出24的表达式,用擅长的语言(C/C++/Java或其他均可)实现程序解决问题。
1.程序风格良好(使用自定义注释模板)
2.列出表达式无重复。

分析♥

  1. 算法分析
    ① 随机生成四个数字
    ② 而根据穷举法列出所有有效式子
  2. 概要设计
    在这里插入图片描述

代码♥

/*
从扑克中每次取出4张牌。
使用加减乘除,第一个能得出24者为赢。(其中,J代表11,Q代表12,K代表13,A代表1),按照要求编程解决24点游戏。
基本要求:
随机生成4个代表扑克牌牌面的数字字母,程序自动列出所有可能算出24的表达式,
 */


import java.util.Random;

public class Main {
    public static void main(String[] args) {
        Random random = new Random();
        int x = random.nextInt(13)+1;
        int y = random.nextInt(13)+1;
        int z = random.nextInt(13)+1;
        int j = random.nextInt(13)+1;
        System.out.println("随机生成1-13数字:");
        System.out.println("第一个数字:" + x);
        System.out.println("第二个数字:" + y);
        System.out.println("第三个数字:" + z);
        System.out.println("第四个数字:" + j);
        System.out.println("可能的结果是:==========");
        Biao(x,y,z,j);

    }

    public static int Biao(int x, int y, int z, int j) {
        if ((x + y * z) * j == 24)
            System.out.println("(" + x + "+" + y + "*" + z + ")" + "*" + j + "=24");
        if ((x * y - z) * j == 24)
            System.out.println("(" + x + "*" + y + "-" + z + ")" + "*" + j + "=24");
        if ((x * y) + z + j == 24)
            System.out.println("(" + x + "*" + y + ")+" + z + "+" + j + "=24");
        if ((x / y - z) * j == 24)
            System.out.println("(" + x + "/" + y + "-" + z + ")" + "*" + j + "=24");
        if ((x - (y - z)) * j == 24)
            System.out.println("(" + x + "-" + "(" + y + "-" + z + ")" + ")*" + j + "=24");
        if (x * y - z + j == 24)
            System.out.println(x + "*" + y + "-" + z + "+" + j + "=24");
        if ((x - y) * z * j == 24)
            System.out.println("(" + x + "-" + y + ")" + "*" + z + "*" + j + "=24");
        if ((x * y) + (z * j) == 24)
            System.out.println("(" + x + "*" + y + ")+(" + z + "*" + j + ")" + "=24");
        if ((x + y) * z * j == 24)
            System.out.println("(" + x + "+" + y + ")" + "*" + z + "*" + j + ")" + "=24");
        if (x * y * z * j == 24)
            System.out.println(x + "*" + y + "*" + z + "*" + j + "=24");
        if (x - (y - z - j) == 24)
            System.out.println(x + "-" + "(" + y + "-" + z + "-" + j + ")" + "=24");
        if (x + y - (z - j) == 24)
            System.out.println(x + "+" + y + "-" + "(" + z + "-" + j + ")" + "=24" + "\n");
        if (x * (y / z - j) == 24)
            System.out.println(x + "*" + "(" + y + "/" + z + "-" + j + ")" + "=24");
        if ((x - y * z) * j == 24)
            System.out.println("(" + x + "-" + y + "*" + z + ")" + "*" + j + "=24");
        if (x * (y - z) + j == 24)
            System.out.println(x + "*" + "(" + y + "-" + z + ")" + "+" + j + "=24");
        if (x + y + z / j == 24)
            System.out.println(x + "+" + y + "+" + z + "/" + j + "=24");
        if ((x - y) * (z - j) == 24)
            System.out.println("(" + x + "-" + y + ")*(" + z + "-" + j + ")=24");
        if ((x + y) * z / j == 24)
            System.out.println("(" + x + "+" + y + ")*" + z + "/" + j + "=24");
        if (x * y / (z - j) == 24)
            System.out.println(x + "*" + y + "/" + "(" + z + "-" + j + ")" + "=24");
        if ((x + y) * (z + j) == 24)
            System.out.println("(" + x + "+" + y + ")*(" + z + "+" + j + ")=24");
        if ((x - y) * z / j == 24)
            System.out.println("(" + x + "-" + y + ")*" + z + "/" + j + "=24");
        if ((x * y - z) / j == 24)
            System.out.println("(" + x + "*" + y + "-" + z + ")" + "/" + j + "=24");
        if ((x + y) / z + j == 24)
            System.out.println("(" + x + "+" + y + "/" + z + "+" + j + "=24");
        if ((x * y) + z - j == 24)
            System.out.println("(" + x + "*" + y + ")+" + z + "-" + j + "=24");
        if ((x / y) + z + j == 24)
            System.out.println("(" + x + "/" + y + ")" + "+" + z + "+" + j + "=24");
        if ((x / y + z) * j == 24)
            System.out.println("(" + x + "/" + y + "+" + z + ")" + "*" + j + "=24");
        if ((x * y) / z / j == 24)
            System.out.println("(" + x + "*" + y + ")/" + z + "/" + j + "=24");
        if (x + y + z - j == 24)
            System.out.println(x + "+" + y + "+" + z + "-" + j + "=24");
        if ((x * y) / z + j == 24)
            System.out.println("(" + x + "*" + y + ")/" + z + "+" + j + "=24");
        if ((x + y - z) * j == 24)
            System.out.println("(" + x + "+" + y + "-" + z + ")*" + j + "=24");
        if ((x * y) - (z - j) == 24)
            System.out.println("(" + x + "*" + y + ")-(" + z + "-" + j + ")" + "=24");
        if ((x * y) - (z + j) == 24)
            System.out.println("(" + x + "*" + y + ")-(" + z + "+" + j + ")" + "=24");
        if ((x - y - z) * j == 24)
            System.out.println("(" + x + "-" + y + "-" + z + ")*" + j + "=24");
        if (x / (y / z - j) == 24)
            System.out.println(x + "/(" + y + "/" + z + "-" + j + ")=24");
        if ((x * y) - (z * j) == 24)
            System.out.println("(" + x + "*" + y + ")-(" + z + "*" + j + ")" + "=24");
        if (((x + y) * z) / j == 24)
            System.out.println("((" + x + y + ")*" + z + ")/" + j + "=24");
        if (x - (y - z) + j == 24)
            System.out.println(x + "-" + "(" + y + "-" + z + ")" + "+" + j + "=24");
        if (x + (y + z) / j == 24)
            System.out.println(x + "+" + "(" + y + "+" + z + ")" + "/" + j + "=24");
        if (x * y / (z + j) == 24)
            System.out.println(x + "*" + y + "/" + "(" + z + "+" + j + ")" + "=24");
        if ((x + y) * z / j == 24)
            System.out.println("(" + x + "+" + y + ")*" + z + "/" + j + "=24");
        if ((x * y + z) * j == 24)
            System.out.println("(" + x + "*" + y + "+" + z + ")" + "*" + j + "=24");
        if (x / (y - z / j) == 24)
            System.out.println(x + "/" + "(" + y + "-" + z + "/" + j + ")" + "=24");
        if (x + (y - z) * j == 24)
            System.out.println(x + "+" + "(" + y + "-" + z + ")*" + j + "=24");
        if ((x + y + z) * j == 24)
            System.out.println("(" + x + "+" + y + "+" + z + ")*" + j + "=24");
        if (x + y * z - j == 24)
            System.out.println(x + "+" + y + "*" + z + "-" + j + "=24");
        if (x * y - z / j == 24)
            System.out.println(x + "*" + y + "-" + z + "/" + j + "=24");
        if ((x + y) * z - j == 24)
            System.out.println("(" + x + "+" + y + ")" + "*" + z + "-" + j + "=24");
        if ((x - y / z) * j == 24)
            System.out.println("(" + x + "-" + y + "/" + z + ")*" + j + "=24");
        if (x * (y + z) + j == 24)
            System.out.println(x + "*" + "(" + y + "+" + z + ")" + "+" + j + "=24");
        if ((x * y) + (z / j) == 24)
            System.out.println("(" + x + "*" + y + ")+(" + z + "/" + j + ")" + "=24");
        if ((x * y) / z - j == 24)
            System.out.println("(" + x + "*" + y + ")/" + z + "-" + j + "=24");
        if ((x + y / z) * j == 24)
            System.out.println("(" + x + "+" + y + "/" + z + ")*" + j + "=24");
        if ((x * y * z) / j == 24)
            System.out.println("(" + x + "*" + y + "*" + z + ")/" + j + "=24");
        if ((x + y * z) / j == 24)
            System.out.println("(" + x + "+" + y + "*" + z + ")/" + j + "=24");
        if (x + (y * z) + j == 24)
            System.out.println(x + "+" + "(" + y + "*" + z + ")+" + j + "=24");
        if (x - (y + z) * j == 24)
            System.out.println(x + "-(" + y + "+" + z + ")*" + j + "=24");
        if (x - (y - z) * j == 24)
            System.out.println(x + "-(" + y + "-" + z + ")*" + j + "=24");
        if ((x * y) - z - j == 24)
            System.out.println("(" + x + "*" + y + ")-" + z + "-" + j + "=24");
        if (x + y / z + j == 24)
            System.out.println(x + "+" + y + "/" + z + "+" + j + "=24");
        if ((x - y) * z - j == 24)
            System.out.println("(" + x + "-" + y + "*" + z + "-" + j + "=24");
        if ((x * y) * z - j == 24)
            System.out.println("(" + x + "*" + y + ")*(" + z + ")-" + j + "=24");
        if ((x * y + z) / j == 24)
            System.out.println("(" + x + "*" + y + "+" + z + ")/" + j + "=24");
        if (x + y + z * j == 24)
            System.out.println(x + "+" + y + "+" + z + "*" + j + "=24");
        if (x * (y - z) / j == 24)
            System.out.println(x + "*" + "(" + y + "-" + z + ")/" + j + "=24");
        if (x / y * z + j == 24)
            System.out.println(x + "/" + y + "*" + z + "+" + j + "=24");
        if (x + y * z * j == 24)
            System.out.println(x + "+" + y + "*" + z + "*" + j + "=24");
        if (x + y + z + j == 24)
            System.out.println(x + "+" + y + "+" + z + "+" + j + "=24");
        if ((x * y) / (z * j) == 24)
            System.out.println("(" + x + "*" + y + ")/(" + z + "*" + j + ")" + "=24");
        if (x + (y + z) * j == 24)
            System.out.println(x + "+(" + y + "+" + z + ")*" + j + "=24");
        if ((x - y) * z + j == 24)
            System.out.println("(" + x + "-" + y + ")" + "*" + z + "+" + j + "=24");
        if ((x + y + z) / j == 24)
            System.out.println("(" + x + "+" + y + "+" + z + ")/" + j + "=24");
        if ((x + y) * z + j == 24)
            System.out.println("(" + x + "+" + y + ")*" + z + "+" + j + "=24");
        return 0;
    }
}

测试

测试没有符合的式子
在这里插入图片描述
测试又符合的式子:
在这里插入图片描述

调试

发现生成随机数字里有0

在这里插入图片描述
在这里插入图片描述

改正方式:查阅资料后发现Random.nextInt(int bound),的区间范围是[0,bound)

在这里插入图片描述

心得体会

解决思路:首先我们应该了解穷举法,所谓穷举法通俗意义上来说就是,列举出所有有可能的情况,在筛选出有效的情况。枚举法是利用计算机运算速度快、精确度高的特点,对要解决问题的所有可能情况,一个不漏地进行检验,从中找出符合要求的答案,因此枚举法是通过牺牲时间来换取答案的全面性。
步骤总结:
首先随机生成4个数字,然后调用有效数字函数,找出适合的式子

  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值