Java实现24点游戏,逻辑简单,去重,判错

这是一个关于使用Java编程实现24点游戏的博客,介绍了如何通过加、减、乘、除运算从四个小于10的数字中得出24的详细逻辑,同时考虑了操作的去重和错误判断。
摘要由CSDN通过智能技术生成

Java实现24点游戏,逻辑简单,去重,判错

最近需要写一个24点游戏,大概意思就是随便给4位小于10的数字,然后利用,加,减,乘,除,把这四位数算出来结果位 24。话不多说直接上代码,直接拷贝就能看效果


import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;


public class Computer {
    public static void main(String[] args) {
        Boolean flag = false;
        System.out.println("请输一个四位数");
        Scanner in = new Scanner(System.in);
        String number = in.nextLine();
        int num = Integer.parseInt(number);
        int a = num / 1000; // 千位
        int b = (num / 100) % 10; // 百位
        int c = (num / 10) % 10;
        int d = num % 10;
        if ((a < 0) || (b < 0) || (c < 0) || (d < 0)) {
            System.out.println("您输入的值不对,请重新输入");
        }
        // 定义一个去重的set集合
        Set<String> stringSet = new HashSet<>();
        // 四种情况排列组合
        flag = permutationAndCombinatio(a, b, c, d, flag, stringSet) || flag;
        flag = permutationAndCombinatio(a, b, d, c, flag, stringSet) || flag;
        flag = permutationAndCombinatio(a, c, d, b, flag, stringSet) || flag;
        flag = permutationAndCombinatio(a, c, b, d, flag, stringSet) || flag;
        flag = permutationAndCombinatio(a, d, b, c, flag, stringSet) || flag;
        flag = permutationAndCombinatio(a, d, c, b, flag, stringSet) || flag;
        flag = permutationAndCombinatio(b, a, c, d, flag, stringSet) || flag;
        flag = permutationAndCombinatio(b, a, d, c, flag, stringSet) || flag;
        flag = permutationAndCombinatio(b, c, a, d, flag, stringSet) || flag;
        flag = permutationAndCombinatio(b, c, d, a, flag, stringSet) || flag;
        flag = permutationAndCombinatio(b, d, c, a, flag, stringSet) || flag;
        flag = permutationAndCombinatio(b, d, a, c, flag, stringSet) || flag;
        flag = permutationAndCombinatio(c, a, b, d, flag, stringSet) || flag;
        flag = permutationAndCombinatio(c, a, d, b, flag, stringSet) || flag
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值