import java.util.Random; public class Test { public int [] random() { int [] intArray =new int[4]; Random random = new Random(); for(int i=0;i<4;i++){ intArray[i] = random.nextInt(13)+1; } return intArray; }
public static void main(String[] args){ Test test= new Test(); int [] intArr =test.random(); while(true){ if((intArr[0]*intArr[1]*intArr[2]*intArr[3])==24){ break; }else{ intArr=test.random(); } } for(int i=0;i<4;i++){ System.out.print(intArr[i]+"\t"); } } }
package com;import java.util.Arrays; import java.util.Scanner;public class Compare { Scanner in = new Scanner(System.in); public static int z = 0; public static int expectedValue; public static int a; public static int b; public static int c; public static int d; public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("请输入四个数字..."); a = in.nextInt(); b = in.nextInt(); c = in.nextInt(); d = in.nextInt(); System.out.println("请输入期望值..."); expectedValue = in.nextInt(); System.out.println("四个数字是:" + a + "," + b + "," + c + "," + d + "..期望值是:expectedValue=" + expectedValue); int[] array = { a, b, c, d }; Arrays.sort(array); StringBuffer buffer = new StringBuffer(); for (int o = 0; o < array.length; o++) { a = array[o]; for (int p = 0; p < array.length; p++) { if (p != o) { b = array[p]; for (int q = 0; q < array.length; q++) { if (q != o && q != p) { c = array[q]; for (int index = 0; index < array.length; index++) { if (index != o && index != p && index != q) { d = array[index]; for (double i = 0; i < 2; i++) { if (i == 0) { for (double j = 0; j < array.length; j++) { for (double k = 0; k < array.length; k++) { fun(oper(a, b, j), arith(c, d, k), j, k, buffer); } } } if (i == 1) { for (int j = 0; j < array.length; j++) { for (int k = 0; k < array.length; k++) { double sum = arith(oper(a, b, j), c, k); func(sum, d, j, k, buffer); } } } } } } } } } } } } private static void func(double i, double j, int j2, int k2, StringBuffer buffer) { String str = ""; String str1 = ""; if (j2 == 0) { str = "+"; } else if (j2 == 1) { str = "-"; } else if (j2 == 2) { str = "*"; } else { str = "/"; } if (k2 == 0) { str1 = "+"; } else if (k2 == 1) { str1 = "-"; } else if (k2 == 2) { str1 = "*"; } else { str1 = "/"; } for (double k = 0; k < 4; k++) { if (k == 0) { if ((i + j) == expectedValue) { String bb = "[(" + a + str + b + ")" + str1 + c + "]" + "+" + d + "=" + expectedValue; if (buffer.indexOf(bb) == -1) { System.out.println(bb); buffer.append(bb); } } } if (k == 1) { if ((i - j) == expectedValue) { String bb = "[(" + a + str + b + ")" + str1 + c + "]" + "-" + d + "=" + expectedValue; if (buffer.indexOf(bb) == -1) { System.out.println(bb); buffer.append(bb); } } } if (k == 2) { if ((i * j) == expectedValue) { String bb = "[(" + a + str + b + ")" + str1 + c + "]" + "*" + d + "=" + expectedValue; if (buffer.indexOf(bb) == -1) { System.out.println(bb); buffer.append(bb); } } } if (k == 3) { if ((i / j) == expectedValue) { String bb = "[(" + a + str + b + ")" + str1 + c + "]" + "/" + d + "=" + expectedValue; if (buffer.indexOf(bb) == -1) { System.out.println(bb); buffer.append(bb); } } } } } private static void fun(double i, double j, double j2, double k2, StringBuffer buffer) { String str = ""; String str1 = ""; if (j2 == 0) { str = "+"; } else if (j2 == 1) { str = "-"; } else if (j2 == 2) { str = "*"; } else { str = "/"; } if (k2 == 0) { str1 = "+"; } else if (k2 == 1) { str1 = "-"; } else if (k2 == 2) { str1 = "*"; } else { str1 = "/"; } for (double k = 0; k < 4; k++) { if (k == 0) { if ((i + j) == expectedValue) { String bb = "(" + a + str + b + ")+(" + c + str1 + d + ")=" + expectedValue; if (buffer.indexOf(bb) == -1) { System.out.println(bb); buffer.append(bb); } } } if (k == 1) { if ((i - j) == expectedValue) { String bb = "(" + a + str + b + ")-(" + c + str1 + d + ")=" + expectedValue; if (buffer.indexOf(bb) == -1) { System.out.println(bb); buffer.append(bb); } } } if (k == 2) { if ((i * j) == expectedValue) { String bb = "(" + a + str + b + ")*(" + c + str1 + d + ")=" + expectedValue; if (buffer.indexOf(bb) == -1) { System.out.println(bb); buffer.append(bb); } } } if (k == 3) { if ((i / j) == expectedValue) { String bb = "(" + a + str + b + ")/(" + c + str1 + d + ")=" + expectedValue; if (buffer.indexOf(bb) == -1) { System.out.println(bb); buffer.append(bb); } } } } } public static double oper(double a, double b, double j) { double z = 0; if (j == 0) { z = a + b; } else if (j == 1) { z = a - b; } else if (j == 2) { z = a * b; } else if (j == 3) { z = a / b; } return z; } public static double arith(double c, double d, double k) { double z = 0; if (k == 0) { z = c + d; } else if (k == 1) { z = c - d; } else if (k == 2) { z = c * d; } else if (k == 3) { z = c / d; } return z; } }直接可以运行的!!!
import java.util.Random;
public class Test {
public int [] random() {
int [] intArray =new int[4];
Random random = new Random();
for(int i=0;i<4;i++){
intArray[i] = random.nextInt(13)+1;
}
return intArray;
}
public static void main(String[] args){
Test test= new Test();
int [] intArr =test.random();
while(true){
if((intArr[0]*intArr[1]*intArr[2]*intArr[3])==24){
break;
}else{
intArr=test.random();
}
}
for(int i=0;i<4;i++){
System.out.print(intArr[i]+"\t");
}
}
}
import java.util.Scanner;public class Compare {
Scanner in = new Scanner(System.in);
public static int z = 0;
public static int expectedValue;
public static int a;
public static int b;
public static int c;
public static int d; public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("请输入四个数字...");
a = in.nextInt();
b = in.nextInt();
c = in.nextInt();
d = in.nextInt();
System.out.println("请输入期望值...");
expectedValue = in.nextInt();
System.out.println("四个数字是:" + a + "," + b + "," + c + "," + d
+ "..期望值是:expectedValue=" + expectedValue);
int[] array = { a, b, c, d };
Arrays.sort(array);
StringBuffer buffer = new StringBuffer();
for (int o = 0; o < array.length; o++) {
a = array[o];
for (int p = 0; p < array.length; p++) {
if (p != o) {
b = array[p]; for (int q = 0; q < array.length; q++) {
if (q != o && q != p) {
c = array[q];
for (int index = 0; index < array.length; index++) {
if (index != o && index != p && index != q) {
d = array[index];
for (double i = 0; i < 2; i++) {
if (i == 0) {
for (double j = 0; j < array.length; j++) {
for (double k = 0; k < array.length; k++) {
fun(oper(a, b, j), arith(c,
d, k), j, k, buffer);
}
}
}
if (i == 1) {
for (int j = 0; j < array.length; j++) {
for (int k = 0; k < array.length; k++) {
double sum = arith(oper(a,
b, j), c, k);
func(sum, d, j, k, buffer);
}
}
}
}
}
}
}
}
}
}
} } private static void func(double i, double j, int j2, int k2,
StringBuffer buffer) {
String str = "";
String str1 = "";
if (j2 == 0) {
str = "+";
} else if (j2 == 1) {
str = "-";
} else if (j2 == 2) {
str = "*";
} else {
str = "/";
}
if (k2 == 0) {
str1 = "+";
} else if (k2 == 1) {
str1 = "-";
} else if (k2 == 2) {
str1 = "*";
} else {
str1 = "/";
}
for (double k = 0; k < 4; k++) {
if (k == 0) {
if ((i + j) == expectedValue) {
String bb = "[(" + a + str + b + ")" + str1 + c + "]" + "+"
+ d + "=" + expectedValue;
if (buffer.indexOf(bb) == -1) {
System.out.println(bb);
buffer.append(bb);
}
}
}
if (k == 1) {
if ((i - j) == expectedValue) {
String bb = "[(" + a + str + b + ")" + str1 + c + "]" + "-"
+ d + "=" + expectedValue;
if (buffer.indexOf(bb) == -1) {
System.out.println(bb);
buffer.append(bb);
}
}
} if (k == 2) {
if ((i * j) == expectedValue) {
String bb = "[(" + a + str + b + ")" + str1 + c + "]" + "*"
+ d + "=" + expectedValue;
if (buffer.indexOf(bb) == -1) {
System.out.println(bb);
buffer.append(bb);
}
}
}
if (k == 3) {
if ((i / j) == expectedValue) {
String bb = "[(" + a + str + b + ")" + str1 + c + "]" + "/"
+ d + "=" + expectedValue;
if (buffer.indexOf(bb) == -1) {
System.out.println(bb);
buffer.append(bb);
}
}
}
}
} private static void fun(double i, double j, double j2, double k2,
StringBuffer buffer) {
String str = "";
String str1 = "";
if (j2 == 0) {
str = "+";
} else if (j2 == 1) {
str = "-";
} else if (j2 == 2) {
str = "*";
} else {
str = "/";
}
if (k2 == 0) {
str1 = "+";
} else if (k2 == 1) {
str1 = "-";
} else if (k2 == 2) {
str1 = "*";
} else {
str1 = "/";
}
for (double k = 0; k < 4; k++) {
if (k == 0) {
if ((i + j) == expectedValue) {
String bb = "(" + a + str + b + ")+(" + c + str1 + d + ")="
+ expectedValue;
if (buffer.indexOf(bb) == -1) {
System.out.println(bb);
buffer.append(bb);
}
}
}
if (k == 1) {
if ((i - j) == expectedValue) {
String bb = "(" + a + str + b + ")-(" + c + str1 + d + ")="
+ expectedValue;
if (buffer.indexOf(bb) == -1) {
System.out.println(bb);
buffer.append(bb);
}
}
} if (k == 2) {
if ((i * j) == expectedValue) {
String bb = "(" + a + str + b + ")*(" + c + str1 + d + ")="
+ expectedValue;
if (buffer.indexOf(bb) == -1) {
System.out.println(bb);
buffer.append(bb);
}
}
}
if (k == 3) {
if ((i / j) == expectedValue) {
String bb = "(" + a + str + b + ")/(" + c + str1 + d + ")="
+ expectedValue;
if (buffer.indexOf(bb) == -1) {
System.out.println(bb);
buffer.append(bb);
}
}
}
}
} public static double oper(double a, double b, double j) {
double z = 0;
if (j == 0) {
z = a + b;
} else if (j == 1) {
z = a - b;
} else if (j == 2) {
z = a * b;
} else if (j == 3) {
z = a / b;
}
return z;
} public static double arith(double c, double d, double k) {
double z = 0;
if (k == 0) {
z = c + d;
} else if (k == 1) {
z = c - d;
} else if (k == 2) {
z = c * d;
} else if (k == 3) {
z = c / d;
}
return z;
}
}直接可以运行的!!!