编辑:我已在该计划的输出中进行了编辑。
该程序要求估计给定的值mu。用户给出mu值,并提供四个不等于1的不同数字(称为w,x,y,z)。然后程序尝试使用de Jaeger公式找到mu值的估计值。
如果我输入mu的值为238,900,w = 14,x = 102329,y = 1936,z = 13 则估计值应为239,103,误差约为0.08%。
我的代码与for循环完美无缺:
public static void main(String[] args) {
SimpleReader in = new SimpleReader1L();
SimpleWriter out = new SimpleWriter1L();
double bestEstimate = 0; // used to hold the estimate the computer while return
double bestA = 0, bestB = 0, bestC = 0, bestD = 0; // to hold the values of the exponents for each number
double[] exponents = { -5, -4, -3, -2, -1, -1.0 / 2.0, -1.0 / 3.0,
-1.0 / 4.0, 0, 1.0 / 4.0, 1.0 / 3.0, 1.0 / 2.0, 1, 2, 3, 4, 5 };
double[] userNumbers = new double[4];
double mu = getPositiveDouble(in, out);
for (int i = 0; i < 4; i++) {
userNumbers[i] = getPositiveDoubleNotOne(in, out);
}
for (int a = 0; a < exponents.length; a++) {
double a1 = Math.pow(userNumbers[0], exponents[a]);
for (int b = 0; b < exponents.length; b++) {
double b1 = Math.pow(userNumbers[1], exponents[b]);
for (int c = 0; c < exponents.length; c++) {
double c1 = Math.pow(userNumbers[2], exponents[c]);
for (int d = 0; d < exponents.length; d++) {
double d1 = Math.pow(userNumbers[3], exponents[d]);
double currentEstimate = a1 * b1 * c1 * d1;
if (Math.abs(mu - currentEstimate) < Math
.abs(mu - bestEstimate)) {
bestEstimate = currentEstimate;
bestA = exponents[a];
bestB = exponents[b];
bestC = exponents[c];
bestD = exponents[d];
}
}
}
}
}
out.println("Best estimate: " + bestEstimate);
out.println(userNumbers[0] + "^" + bestA + ", " + userNumbers[1] + "^"
+ bestB + ", " + userNumbers[2] + "^" + bestC + ", "
+ userNumbers[3] + "^" + bestD);
out.println("Error: " + calculateError(mu, bestEstimate) * 100 + "%");
}
输出:
Enter a positive real number: 238900
Enter a positive real number that isn't 1: 14
Enter a positive real number that isn't 1: 102329
Enter a positive real number that isn't 1: 1936
Enter a positive real number that isn't 1: 13
Best estimate: 239102.78648033558
14.0^-5.0, 102329.0^1.0, 1936.0^0.5, 13.0^4.0
Error: 0.08488341579555334%
然而,使用while循环,我无法复制它。
while (a < exponents.length) {
double a1 = Math.pow(userNumbers[0], exponents[a]);
while (b < exponents.length) {
double b1 = Math.pow(userNumbers[1], exponents[b]);
while (c < exponents.length) {
double c1 = Math.pow(userNumbers[2], exponents[c]);
while (d < exponents.length) {
double d1 = Math.pow(userNumbers[3], exponents[d]);
double currentEstimate = a1 * b1 * c1 * d1;
if (Math.abs(mu - currentEstimate) < Math
.abs(mu - bestEstimate)) {
bestEstimate = currentEstimate;
bestA = exponents[a];
bestB = exponents[b];
bestC = exponents[c];
bestD = exponents[d];
}
d++;
}
c++;
}
b++;
}
a++;
}
输出:
Enter a positive real number: 238900
Enter a positive real number that isn't 1: 14
Enter a positive real number that isn't 1: 102329
Enter a positive real number that isn't 1: 1936
Enter a positive real number that isn't 1: 13
Best estimate: 0.0
14.0^0.0, 102329.0^0.0, 1936.0^0.0, 13.0^0.0
答案 0 :(得分:1)
您尚未初始化接下来几次迭代的变量。
您需要在各自的while循环之外重新初始化用于while循环条件检查的变量。即
b = 0;
while(b < exponents.length){
}
类似地对使用变量c
&amp;的while循环执行此操作。 d
。
答案 1 :(得分:0)
Daniel's answer is correct:
while
循环的结构应为:
int a=0, b=0, c=0, d=0;
while (a < length) {
b=0;
while (b < length) {
c=0;
while (c < length) {
d=0;
while (d < length) {
d++;
}
c++;
}
b++;
}
a++;
}