We use the random function , Random from 1~13 Pick a number from the list , Choose four times .
public static void main(String[] args) { Random r = new Random(); int r1 = r.
nextInt(13) + 1; int r2 = r.nextInt(13) + 1; int r3 = r.nextInt(13) + 1; int r4
= r.nextInt(13) + 1; int[] num = new int[]{r1, r2, r3, r4}; }
To make it more like a card , We will 1 Transform to A, take 11,12,13 Transform to J,Q,K.

You can create one changeNum method .
public static String changeNum(int num) { if (num == 1) { return "A"; } else if
(num == 11) { return "J"; } else if (num == 12) { return "Q"; } else if (num ==
13) { return "K"; } else { return "" + num; } }
Such a randomly generated 24 Just click on the game , Just at the beginning psvm Just output four results at .
such as
System.out.println(changeNum(r1) + " " + changeNum(r2) + " " + changeNum(r3) +
" " + changeNum(r4));
J 8 8 Q ( Randomly generated )

Then we need to find a way to make a program that can solve the problem on our own . The advantage of computer lies in its speed , So we can use the exhaustion method .

First, create an unknown , use finalNum name . Finally, let's make it work for you 24 Output the whole formula .

Then we need to create three variables , namely math1,math2,math3, A symbol used to represent addition, subtraction, multiplication and division .

I tried multilayer for loop nesting , The purpose is also to achieve the exhaustive method of addition, subtraction, multiplication and division . But when I finished writing the code , I find it can only be counted in order .

such as

J 8 8 Q

It'll count

-> ((11+8)/8)*12
mean : -> ((J+8)/8)*Q

be careful ,java In the operator of , If used int Variable calculation , You can't get decimals by division .5/2=2 Instead of 2.5. So we can add it before the formula (double) take int Convert to double( Inspiration from Britain ).

It can't change the order , Such as :

-> ((11* 12)/8)+8
mean : -> ((J*Q)/8)+8

So how to solve this problem , Become the focus of the next program design .

I added two more layers to the original nesting for loop nesting , The code is as follows :
for (int l = 0; l < 4; l++) { for (int m = 1; m < 4 - l; m++) { //first if (i
== 0) { finalNum = num[l] + num[l + m]; math1 = "+"; } else if (i == 1) {
finalNum= num[l] - num[l + m]; math1 = "-"; } else if (i == 2) { finalNum = num[
l] * num[l + m]; math1 = "*"; } else { finalNum = num[l] / num[l + m]; math1 =
"/"; }
such , We just need to make the two numbers in the following operation not sum num[l] as well as num[l+m] Just crash .

I use this way of writing :
if (n != l && n != l + m)
The overall code is further refined in general details , All of them are as follows :
package S1; import java.util.Random; public class S1 { public static void main(
String[] args) { Random r = new Random(); int r1 = r.nextInt(13) + 1; int r2 = r
.nextInt(13) + 1; int r3 = r.nextInt(13) + 1; int r4 = r.nextInt(13) + 1; int[]
num= new int[]{r1, r2, r3, r4}; System.out.println(changeNum(r1) + " " +
changeNum(r2) + " " + changeNum(r3) + " " + changeNum(r4)); calculator(num); }
public static String changeNum(int num) { if (num == 1) { return "A"; } else if
(num == 11) { return "J"; } else if (num == 12) { return "Q"; } else if (num ==
13) { return "K"; } else { return "" + num; } } public static void calculator(
int[] num) { double finalNum = 0; String math1 = "", math2 = "", math3 = ""; int
times= 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { for (int
k= 0; k < 4; k++) { for (int l = 0; l < 4; l++) { for (int m = 1; m < 4 - l; m++
) { //first if (i == 0) { finalNum = num[l] + num[l + m]; math1 = "+"; } else if
(i == 1) { finalNum = num[l] - num[l + m]; math1 = "-"; } else if (i == 2) {
finalNum= num[l] * num[l + m]; math1 = "*"; } else { finalNum = (double)num[l] /
num[l + m]; math1 = "/"; } //second int num1; int num2; for (int n = 0; n < 4;
n++) { if (n != l && n != l + m) { num1 = num[n]; if (j == 0) { finalNum += num1
; math2 = "+"; } else if (j == 1) { finalNum -= num1; math2 = "-"; } else if (j
== 2) { finalNum *= num1; math2 = "*"; } else { finalNum /= num1; math2 = "/"; }
for (int o = 3; o > n; o--) { if (o != l && o != l + m) { num2 = num[o]; //third
if (k == 0) { finalNum += num2; math3 = "+"; } else if (k == 1) { finalNum -=
num2; math3 = "-"; } else if (k == 2) { finalNum *= num2; math3 = "*"; } else {
finalNum/= num2; math3 = "/"; } if (finalNum == 24) { times += 1; System.out.
println(" Find number one " + times + " A solution :"); System.out.println(" -> ((" + num[l] + "" +
math1+ num[l + m] + ")" + math2 + num1 + ")" + math3 + num2); System.out.println
(" mean :" + " -> ((" + changeNum(num[l]) + "" + math1 + changeNum(num[l + m]) + ")"
+ math2 + changeNum(num1) + ")" + math3 + changeNum(num2)); System.out.println()
; } } } } } } } } } } } }
The test results are as follows :

K 3 K 8
Find number one 1 A solution :
-> ((13 + 3) - 13) * 8
mean : -> ((K + 3) - K ) * 8
Find number one 2 A solution :
-> ((3 + 13) - 13) * 8
mean : -> ((3 + K) - K ) * 8
Find number one 3 A solution :
-> ((13 + 8) - 13) * 3
mean : -> ((K + 8) - K ) * 3
Find number one 4 A solution :
-> ((13 - 13) + 3) * 8
mean : -> ((K - K) + 3 ) * 8
Find number one 5 A solution :
-> ((3 - 13) + 13) * 8
mean : -> ((3 - K) + K ) * 8
Find number one 6 A solution :
-> ((3 * 8) + 13) - 13
mean : -> ((3 * 8) + K ) - K
Find number one 7 A solution :
-> ((3 * 8) - 13) + 13
mean : -> ((3 * 8) - K ) + K
Find number one 8 A solution :
-> ((13 * 8) * 3) / 13
mean : -> ((K * 8) * 3 ) / K
Find number one 9 A solution :
-> ((3 * 8) * 13) / 13
mean : -> ((3 * 8) * K ) / K
Find number one 10 A solution :
-> ((13 * 3) / 13) * 8
mean : -> ((K * 3) / K ) * 8
Find number one 11 A solution :
-> ((3 * 13) / 13) * 8
mean : -> ((3 * K) / K ) * 8
Find number one 12 A solution :
-> ((3 * 8) / 13) * 13
mean : -> ((3 * 8) / K ) * K
Find number one 13 A solution :
-> ((13 * 8) / 13) * 3
mean : -> ((K * 8) / K ) * 3
Find number one 14 A solution :
-> ((13 / 13) * 3) * 8
mean : -> ((K / K) * 3 ) * 8
Find number one 15 A solution :
-> ((3 / 13) * 13) * 8
mean : -> ((3 / K) * K ) * 8

K 7 5 K
Find number one 1 A solution :
-> ((5 + 13) + 13) - 7
mean : -> ((5 + K) + K) - 7
Find number one 2 A solution :
-> ((13 + 5) - 7) + 13
mean : -> ((K + 5) - 7) + K
Find number one 3 A solution :
-> ((13 + 13) - 7) + 5
mean : -> ((K + K) - 7) + 5
Find number one 4 A solution :
-> ((13 - 7) + 5) + 13
mean : -> ((K - 7) + 5) + K

6 3 5 3
Find number one 1 A solution :
-> ((6 + 5) - 3) * 3
mean : -> ((6 + 5) - 3) * 3
Find number one 2 A solution :
-> ((6 - 3) + 5) * 3
mean : -> ((6 - 3) + 5) * 3
Find number one 3 A solution :
-> ((5 - 3) + 6) * 3
mean : -> ((5 - 3) + 6) * 3
Find number one 4 A solution :
-> ((3 * 5) + 6) + 3
mean : -> ((3 * 5) + 6) + 3
Find number one 5 A solution :
-> ((5 * 3) + 6) + 3
mean : -> ((5 * 3) + 6) + 3
Find number one 6 A solution :
-> ((6 * 5) - 3) - 3
mean : -> ((6 * 5) - 3) - 3

7 A 5 K
Find number one 1 A solution :
-> ((5 + 13) + 7) - 1
mean : -> ((5 + K) + 7) - A
Find number one 2 A solution :
-> ((7 + 5) - 1) + 13
mean : -> ((7 + 5) - A) + K
Find number one 3 A solution :
-> ((7 + 13) - 1) + 5
mean : -> ((7 + K) - A) + 5
Find number one 4 A solution :
-> ((7 - 1) + 5) + 13
mean : -> ((7 - A) + 5) + K


//( Do not know why? CSDN Multiple sign of * somewhat bug, Errors are often displayed , So in some cases, I put spaces on both sides of the multiply sign )

I'm still learning , The code must not be optimized enough or even exist bug. I also hope you can correct my criticism , learn from each other .

==================== 2021.3.27 to update , take :
public static void main(String[] args) { Random r = new Random(); int r1 = r.
nextInt(13) + 1; int r2 = r.nextInt(13) + 1; int r3 = r.nextInt(13) + 1; int r4
= r.nextInt(13) + 1; int[] num = new int[]{r1, r2, r3, r4}; }
Change to :
public static void main(String[] args) { Scanner s = new Scanner(System.in);
System.out.println(" Please enter the first number "); int r1 = s.nextInt(); System.out.println(
" Please enter the second number "); int r2 = s.nextInt(); System.out.println(" Please enter the third number "); int r3 = s.
nextInt(); System.out.println(" Please enter the fourth number "); int r4 = s.nextInt(); int[] num = new
int[]{r1, r2, r3, r4}; System.out.println(changeNum(r1) + " " + changeNum(r2) +
" " + changeNum(r3) + " " + changeNum(r4)); calculator(num); }
I can do the title, ha ha ha .

such as apple watch Of 24 Point game app:

We input :

You can cheat ~

Technology
©2019-2020 Toolsou All rights reserved,
2020 The 11th National Blue Bridge Cup C/C++b Group summary ( Completion ) Review of the most complete computer network principles in history vue-cli 3 VUE Scaffold project construction ( Detailed explanation ) solve Vue+TypeScript Under development TS Don't recognize this.$refs The question of Vue Using the function of anti chattering and throttling How to use division operation in relational algebra SQL Statement representation ?copy-webpack-plugin Copy and compress files avue The use of dictionaries in English Teaching girls to learn Java: What is? Java?python Code painting Cherry Blossom -python Draw cherry tree code Specific code introduction