早教吧 育儿知识 作业答案 考试题库 百科 知识分享

随机输入四个数字,用加减乘除得到期望值,用java语言比如说我输入1234,我的期望值是10,那么有1+2+3+4=10,还有3*4-1*2=10就是要把输入的数字的各个位通过加减乘除得到输入的期望值的这个数字.

题目详情
随机输入四个数字,用加减乘除得到期望值,用java语言
比如说 我输入1234,我的期望值是10,那么有1+2+3+4=10,还有3*4-1*2=10 就是要把输入的数字的各个位通过加减乘除得到输入的期望值的这个数字.
希望有人帮忙,很着急,最好今天下午能写好~希望有些注释,我的java水平不是十分高,
如果有人写的好可以追加悬赏~
▼优质解答
答案和解析
package test;
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;
}
}
测试数据:
请输入四个数字...
1
1
9
9
请输入期望值...
10
四个数字是:1,1,9,9..期望值是:expectedValue=10
[(1/9)+1]*9=10