import java.util.Random;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class test {
public static void main(String[] args) throws ScriptException {
for (int l2 = 0; l2 < 500; l2++) {
int o1 = (int) (Math.random() * 4 + 1);
int o2 = (int) (Math.random() * 4 + 1);
int o3 = (int) (Math.random() * 4 + 1);
int k = (int) (Math.random() * 4 + 1);
double k1 = 0;
double k2 = 0;
double k3 = 0;
double k4 = 0;
if (k == 1) {
k1 = 1;
k2 = 5;
k3 = 5;
k4 = 5;
} else if (k == 2) {
k1 = 5;
k2 = 1;
k3 = 5;
k4 = 5;
} else if (k == 3) {
k1 = 5;
k2 = 5;
k3 = 1;
k4 = 5;
} else if (k == 4) {
k1 = 5;
k2 = 5;
k3 = 5;
k4 = 1;
}
double sumM = getsum(k1, getOp(o1), k2);
double sumM1 = getsum(sumM, getOp(o2), k3);
double sumM2 = getsum(sumM1, getOp(o3), k4);
if (sumM2 == 24) {
System.out.println(k1 + " " + getOp(o1) + " " + k2 + " " + getOp(o2) + " " + k3 + " " + getOp(o3) + " "
+ k4 + "=" + sumM2);
}
}
System.out.println("结束了");
}
static ScriptEngine jse = new ScriptEngineManager().getEngineByName("JavaScript");
private static String getOp(int k) {
String str = "";
switch (k) {
case 1:
str = "+";
break;
case 2:
str = "-";
break;
case 3:
str = "*";
break;
case 4:
str = "/";
break;
}
return str;
}
private static double getsum(double k1, String b, double k2) {
double str = 0;
Random ran = new Random();
int num = ran.nextInt(2) + 1;
if (num == 1) {
try {
str = Double.parseDouble(String.valueOf(jse.eval((k1 + b + k2).replaceAll("--", "+"))));
} catch (ScriptException e) {
e.printStackTrace();
}
} else {
try {
str = Double.parseDouble(String.valueOf(jse.eval((k2 + b + k1).replaceAll("--", "+"))));
} catch (ScriptException e) {
e.printStackTrace();
}
}
return str;
}
}