第一种:
循环:
for (int x = 1; x <= 500; x++) {
int y = 500 - x; // x为基本的 y为额外
if (0.15 * x + 0.07 * y == 75) {
System.out.println("x:" + x);
System.out.println("y:" + y);
break;
}
}
第二种:
BigDecimal
double x = 0;
double y = 0;
BigDecimal xBig = new BigDecimal(x);
BigDecimal yBig = new BigDecimal(y);
BigDecimal result = new BigDecimal(14);
BigDecimal baseRat = new BigDecimal(0.0467);
BigDecimal extraRat = new BigDecimal(0.0133);
BigDecimal mul = new BigDecimal(300);
BigDecimal a = result.subtract(extraRat.multiply(mul)).setScale(3,
BigDecimal.ROUND_HALF_UP);
BigDecimal b = baseRat.subtract(extraRat).setScale(3,
BigDecimal.ROUND_HALF_UP);
xBig = a.divide(b, 3, BigDecimal.ROUND_HALF_UP);
yBig = mul.subtract(xBig);
System.out.println("xBig:" + xBig);
System.out.println("yBig:" + yBig);
第三种:
int x = 0, y = 0;
label: for (int i = 0; i < 900; i++) {
y = i;
for (int j = 0; j < 4200; j++) {
x = j;
if ((x + y) == 5000 && 633 * x + 133 * y == 2747000) {
System.out.println("结果:x = " + x + ",y = " + y
+ ",x + Y = " + (x + y));
break label;
}
}
}