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