java24点游戏代码_Java代码实现游戏24点

package cn.chetech.suanfa;

import java.util.Scanner;

/**

* 24点游戏是一个大众化的益智游戏。任意给4张扑克牌(不包括大小王),只能够用加、减、乘、除以及适当的括号连接四张牌

* 无论顺序,使计算结果为24,或者宣布根本就是无解的,每张牌必须运算,并且只能运算一次, J,Q,K可设置为11,12,13

* 1、计算值是否24,四个变量去代替a,b,c,d,中间相邻的符号op1,op2,op3, a op1 b op2 c op3 d =

* 24,符号运算还有优先级的问题,用括号去提升它的优先级 优先级的几种情况: 1、(a op1 b)op2 (c op3 d) 2、(( a op1

* b)op2 c) op3 d 3、a op1 ((b op2 c) op3 d) 4、a op1 (b op2 (c op3 d)) 5、(a op1

* (b op2 c) op3 d) op1 的取值范围加 ,减,乘,除 每次给4张牌,有多少种情况 ,64*5 = 320种,

* 在320种找出计算结果为24的表达式打印出来

*/

public class TwentyFour {

// 存储运算符

private final static char[] op = { '#', '+', '-', '*', '/' };

// 计算 x op y

private static float cal(float x, float y, int op) {

float result = 0; // 保存结果

switch (op) {

case 1:

result = x + y;

break;

case 2:

result = x - y;

break;

case 3:

result = x * y;

break;

case 4:

result = x / y;

break;

}

return result;

}

// 1、(a op1 b)op2 (c op3 d)

private static float cal_expressiona1(float a, float b, float c, float d, int op1, int op2, int op3) {

float r1, r2, r3;

r1 = cal(a, b, op1);

r2 = cal(c, d, op3);

r3 = cal(r1, r2, op2);

return r3;

}

// 2、(( a op1 b)op2 c) op3 d

private static float cal_expressiona2(float a, float b, float c, float d, int op1, int op2, int op3) {

float r1, r2, r3;

r1 = cal(a, b, op1);

r2 = cal(r1, c, op2);

r3 = cal(r2, d, op3);

return r3;

}

// 3、a op1 ((b op2 c) op3 d)

private static float cal_expressiona3(float a, float b, float c, float d, int op1, int op2, int op3) {

float r1, r2, r3;

r1 = cal(b, c, op2);

r2 = cal(r1, d, op3);

r3 = cal(a, r2, op1);

return r3;

}

// 4、a op1 (b op2 (c op3 d))

private static float cal_expressiona4(float a, float b, float c, float d, int op1, int op2, int op3) {

float r1, r2, r3;

r1 = cal(c, d, op3);

r2 = cal(b, r1, op2);

r3 = cal(a, r2, op1);

return r3;

}

// 5、(a op1 (b op2 c) op3 d)

private static float cal_expressiona5(float a, float b, float c, float d, int op1, int op2, int op3) {

float r1, r2, r3;

r1 = cal(b, c, op2);

r2 = cal(a, r1, op1);

r3 = cal(r2, d, op3);

return r3;

}

static boolean get(int a,int b,int c, int d){

boolean flag=false; // 判断24标志 未得到就是false

// int op1,op2,op3;

for(int op1 = 1;op1<=4;op1++){

for(int op2 = 1;op2<=4;op2++){

for(int op3 = 1;op3<=4;op3++){

// 调用表达式1的方法

if(cal_expressiona1(a, b, c, d, op1, op2, op3)==24){

System.out.println("("+a+op[op1]+b +")"+ op[op2] +"( "+c+op[op3]+d+")"+" = 24");

flag=true;

}

// 调用表达式2的方法 (( a op1 b)op2 c) op3 d

if(cal_expressiona2(a, b, c, d, op1, op2, op3)==24){

System.out.println("(("+a+op[op1]+b +")"+ op[op2]+ c +")"+op[op3]+d+" = 24");

flag=true;

}

// 调用表达式3的方法 a op1 ((b op2 c) op3 d)

if(cal_expressiona3(a, b, c, d, op1, op2, op3)==24){

System.out.println("( "+a+op[op1]+"(("+b+ op[op2]+ c +")"+op[op3]+d +")) = 24");

flag=true;

}

// 调用表达式4的方法 (a op1 (b op2 (c op3 d)))

if(cal_expressiona4(a, b, c, d, op1, op2, op3)==24){

System.out.println("( "+a+op[op1]+"("+b+ op[op2]+ "(" +c +op[op3]+d +"))) = 24");

flag=true;

}

// 调用表达式5的方法 (a op1 (b op2 c) op3 d)

if(cal_expressiona5(a, b, c, d, op1, op2, op3)==24){

System.out.println("("+a+op[op1]+"("+b+ op[op2]+ c +")"+op[op3]+d +") = 24");

flag=true;

}

}

}

}

return flag;

}

public static void main(String[] args) {

int a,b,c,d;

Scanner in = new Scanner(System.in);

System.out.println("请输入四个数(1~13):");

do {

a = in.nextInt();

b = in.nextInt();

c = in.nextInt();

d = in.nextInt();

if(a<1 || a>13 ||b<1||b>13 || c<1 || c>13 || d<1 ||d>13)

System.out.println("输入错误,请重新输入!");

} while (a<1 || a>13 ||b<1||b>13 || c<1 || c>13 || d<1 ||d>13);

if(!get(a,b,c,d))

System.out.println("对不起,这四个数得不到24");

in.close();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值