gpt4 book ai didi

java - 程序适用于 for 循环,但不适用于 while 循环?

转载 作者:行者123 更新时间:2023-11-30 06:18:51 124 4
gpt4 key购买 nike

编辑:我已经在程序的输出中进行了编辑。

该程序要求估计给定值 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

最佳答案

您尚未初始化接下来几次迭代的变量。

您需要在各自的 while 循环之外重新初始化用于 while 循环条件检查的变量。即

b = 0;
while(b < exponents.length){
}

对使用变量 cd 的 while 循环执行类似的操作。

关于java - 程序适用于 for 循环,但不适用于 while 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48592875/

124 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com