24点算法java_Java24点计算24是流行的扑克游戏.其方法是任意取出4张牌,AJQK王牌算1,其它牌按点数计算,花色不计.目标是通过加、减、乘、除和括号最终算出24.设计一个程序,输入4个数字...

Java24点

public class Test2

{

public void Fun(int a,int b,int c,int d)

{

int op1,op2,op3 = 0;

String x = null,y = null,z = null;

double n = 0;

for (op1 = 0; op1 < 4; op1++)

{

switch (op1)

{

case 0:

n = a + b;

x = "+";

break;

case 1:

n = a - b;

x = "-";

break;

case 2:

n = a * b;

x = "*";

break;

case 3:

n = a / b;

x = "/";

break;

}

for (op2 = 0; op2 < 4; op2++)

{

switch (op2)

{

case 0:

n = n + c;

y = "+";

break;

case 1:

n = n - c;

y = "-";

break;

case 2:

n = n * c;

y = "*";

break;

case 3:

n = n / c;

y = "/";

break;

}

for (op3 = 0; op3 < 4; op3++)

{

switch (op3)

{

case 0:

n = n + d;

z = "+";

break;

case 1:

n = n - d;

z = "-";

break;

case 2:

n = n * d;

z = "*";

break;

case 3:

n = n / d;

z = "/";

break;

}

if (n == 24.0)

{

System.out.println("(" + "(" + a + x + b + ")" + y + c

+ ")" + z + d + ")");

System.out.println("---------------------------");

}

}

}

}

}

public void Scan()

{

int n = 0;

while (n < 10000)

{

int a = (int) ((Math.random() * 9) + 1);

int b = (int) ((Math.random() * 9) + 1);

int c = (int) ((Math.random() * 9) + 1);

int d = (int) ((Math.random() * 9) + 1);

Fun(a,b,c,d);

n++;

}

}

/**

* @param args

*/

public static void main(String[] args)

{

Test2 t = new Test2();

t.Fun(1,2,3,4);

t.Fun(4,6,9,5);

t.Scan();

}

}

((4*2)*3)-5)

---------------------------

((4*2)*3)/5)

03-27 397
11-03 7140
04-14 682
04-13 6598
10-04 2785
01-23 6289

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

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

¥2 ¥4 ¥6 ¥10 ¥20

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