我试图让我的程序运行四个嵌套的while循环,通过从数组中绘制来计算用户输入数字的相对误差{-5,-4,-3,-2,-1,-1 / 2,-1 / 3,-1 / 4,0,1 / 4,1 / 3,1 / 2,1,2,3,4,5}。以下是此作业的摘录:
“考虑de jager公式w ^ ax ^ by ^ cz ^ d,其中a,b,c和d中的每一个是17个数字中的一个{-5,-4,-3,-2, - 1,-1 / 2,-1 / 3,-1 / 4,0,1 / 4,1 / 3,1 / 2,1,2,3,4,5}。“迷人的理论”断言,具有四个个人数字的de Jager公式可用于在1%相对误差的一小部分内近似μ。例如,假设您选择以英里为单位近似从地球到月球的平均距离:μ= 238,900。并假设您是俄勒冈州立大学的体育迷,所以你的个人数字是俄勒冈州立大学上一个全国冠军赛季的胜利数(14;也是任何大学队一年的胜利纪录),俄亥俄体育场的座位数(102,329),年度Jesse Owens在柏林(1936年)获得四枚金牌,以及你在打高中曲棍球时的球衣号码(13)。然后14-5102329119361 / 2134的值约为239,103,约为μ的0.08%。
您的工作是创建一个Java程序,询问用户应该近似的常数μ,然后依次询问四个个人数字w,x,y和z中的每一个。然后,程序应计算并报告指数a,b,c和d的值,使de jager公式尽可能接近μ,以及公式waxbyczd的值和近似值的相对误差。最接近的百分之一。“
以下代码是我目前所拥有的。我意识到它不是在当前状态下工作但我希望我至少在它的正确轨道上。我不知道从这里去哪里或如何修复我的嵌套循环。非常感谢任何帮助,因为我真的在努力应该如何工作。我也意识到for循环对于这种嵌套循环会更好,但我需要先使用while循环才能使用。
import components.simplereader.SimpleReader;
import components.simplereader.SimpleReader1L;
import components.simplewriter.SimpleWriter;
import components.simplewriter.SimpleWriter1L;
public final class ABCDGuesser1 {
private ABCDGuesser1() {
}
public static void main(String[] args) {
SimpleReader in = new SimpleReader1L();
SimpleWriter out = new SimpleWriter1L();
out.print("Enter a positive real-valued universal physical or mathematical constant: ");
double mu = in.nextDouble();
out.print("Enter four postitive numbers not equal to 1: ");
double w = in.nextDouble();
double x = in.nextDouble();
double y = in.nextDouble();
double z = in.nextDouble();
int[] charm = { -5, -4, -3, -2, -1, -1 / 2, -1 / 3, -1 / 4, 0, 1 / 4,
1 / 3, 1 / 2, 1, 2, 3, 4, 5 };
int a = 0;
int i = 0;
while (i < charm.length) {
a = charm[i];
double max1 = (Math.pow(w, a));
i++;
int b = 0;
int j = 0;
while (j < charm.length) {
b = charm[j];
double max2 = (Math.pow(x, b));
j++;
int c = 0;
int k = 0;
while (k < charm.length) {
c = charm[k];
double max3 = (Math.pow(y, c));
k++;
int d = 0;
int n = 0;
while (n < charm.length) {
d = charm[n];
double max4 = (Math.pow(z, d));
n++;
}
out.print(max1);
}
}
}
in.close();
out.close();
}
}
答案 0 :(得分:0)
如Invexity所述,double [] charm,而不是int [] charm
循环时,跟踪bestEstimate
所以你的代码代码是
package area51;
import java.io.PrintStream;
public final class ABCDGuesser1 {
private ABCDGuesser1() {
}
public static void main(String[] args) {
PrintStream out = System.out;
//out.print("Enter a positive real-valued universal physical or mathematical constant: ");
double mu = 2.567;
//out.print("Enter four postitive numbers not equal to 1: ");
double w = 123.4;
double x = 2.567;
double y = 8.9;
double z = 12.6;
double[] charm = { -5, -4, -3, -2, -1, -1 / 2, -1 / 3, -1 / 4, 0, 1 / 4, 1 / 3, 1 / 2, 1, 2, 3, 4, 5 };
double bestEstimate = 0;
String bestIndexes = "";
for (int i = 0; i < charm.length; i++) {
double max1 = (Math.pow(w, charm[i]));
for (int j = 0; j < charm.length; j++) {
double max2 = (Math.pow(x, charm[j]));
for (int k = 0; k < charm.length; k++) {
double max3 = (Math.pow(y, charm[k]));
for (int n = 0; n < charm.length; n++) {
double max4 = (Math.pow(z, charm[n]));
double currentEstimate = max1 * max2 * max3 * max4;
if (Math.abs(mu - currentEstimate) < Math.abs(mu - bestEstimate)) {
bestEstimate = currentEstimate;
bestIndexes = ""+i+","+j+","+k+","+n;
}
}
//out.print(max1);
}
}
}
out.println("Best Estimate = " + bestEstimate);
out.println("Best Indexes = " + bestIndexes);
}
}
答案 1 :(得分:0)
/*Could you tell me why these two programs give different outputs? The only difference between them that I can see is that the first uses while loops with no method, while the second uses for loops and a main method. I don't want to change the for loop program anymore if I have to, I just want the while loop program's output to match that of the for loop one. The output for the first program is:
Enter a positive real-valued universal physical or mathematical constant: 238900
Enter four postitive number not equal to 1: 14
102329
1936
13
Most appropriate exponents for w, x, y and z: 1 14 6 12
Best Estimate = 253104.94721879277
Relative error = 0.059459804180798555%
Exception in thread "main" java.lang.AssertionError: Violation of: this is open
at components.simplewriter.SimpleWriter1L.println(SourceFile:177)
at ABCDGuesser1.main(ABCDGuesser1.java:93)
Second program:
Enter a positive real-valued universal physical or mathematical constant: 238900
Enter four postitive number not equal to 1: 14
102329
1936
13
Most appropriate exponents for w, x, y and z: -4.0 2.0 1.0 -3.0
Best Estimate = 240193.14762851998
Relative error = 0.0054129243554624324
*/
public static void main(String[] args) {
SimpleReader in = new SimpleReader1L();
SimpleWriter out = new SimpleWriter1L();
out.print("Enter a positive real-valued universal physical or mathematical constant: ");
double mu = in.nextDouble();
out.print("Enter four postitive number not equal to 1: ");
double w = in.nextDouble();
double x = in.nextDouble();
double y = in.nextDouble();
double z = in.nextDouble();
double[] charm = { -5, -4, -3, -2, -1, -1 / 2, -1 / 3, -1 / 4, 0,
1 / 4, 1 / 3, 1 / 2, 1, 2, 3, 4, 5 };
double bestEstimate = 0;
double a, b, c, d;
double bestIndex1 = 0;
double bestIndex2 = 0;
double bestIndex3 = 0;
double bestIndex4 = 0;
int i = 0;
while (i < charm.length) {
a = (Math.pow(w, charm[i]));
i++;
int j = 0;
while (j < charm.length) {
b = (Math.pow(x, charm[j]));
j++;
int k = 0;
while (k < charm.length) {
c = (Math.pow(y, charm[k]));
k++;
int n = 0;
while (n < charm.length) {
d = (Math.pow(z, charm[n]));
double currentEstimate = a * b * c * d;
if (Math.abs(mu - currentEstimate) < Math.abs(mu
- bestEstimate)) {
bestEstimate = currentEstimate;
bestIndex1 = charm[i];
bestIndex2 = charm[j];
bestIndex3 = charm[k];
bestIndex4 = charm[n];
}
n++;
}
}
}
double relError = Math.abs((mu - bestEstimate) / mu);
out.println("Most appropriate exponents for w, x, y and z: "
+ bestIndex1 + " " + bestIndex2 + " " + bestIndex3 + " "
+ bestIndex4);
out.println("Best Estimate = " + bestEstimate);
out.println("Relative error = " + relError + "%");
in.close();
out.close();
}
}
private static double relError(double constant, double fin) {
double estimate = Math.abs((constant - fin) / constant);
return estimate;
}
/**
* Main method.
*
* @param args
* the command line arguments
*/
public static void main(String[] args) {
SimpleReader in = new SimpleReader1L();
SimpleWriter out = new SimpleWriter1L();
out.print("Enter a positive real-valued universal physical or mathematical constant: ");
double mu = in.nextDouble();
out.print("Enter four postitive number not equal to 1: ");
double w = in.nextDouble();
double x = in.nextDouble();
double y = in.nextDouble();
double z = in.nextDouble();
double[] charm = { -5, -4, -3, -2, -1, -1 / 2, -1 / 3, -1 / 4, 0,
1 / 4, 1 / 3, 1 / 2, 1, 2, 3, 4, 5 };
double bestEstimate = 0;
double bestIndex1 = 0;
double bestIndex2 = 0;
double bestIndex3 = 0;
double bestIndex4 = 0;
for (int i = 0; i < charm.length; i++) {
double a = (Math.pow(w, charm[i]));
for (int j = 0; j < charm.length; j++) {
double b = (Math.pow(x, charm[j]));
for (int k = 0; k < charm.length; k++) {
double c = (Math.pow(y, charm[k]));
for (int n = 0; n < charm.length; n++) {
double d = (Math.pow(z, charm[n]));
double currentEstimate = a * b * c * d;
if (Math.abs(mu - currentEstimate) < Math.abs(mu
- bestEstimate)) {
bestEstimate = currentEstimate;
bestIndex1 = charm[i];
bestIndex2 = charm[j];
bestIndex3 = charm[k];
bestIndex4 = charm[n];
}
}
}
}
}
double error = relError(mu, bestEstimate);
out.println("Most appropriate exponents for w, x, y and z: "
+ bestIndex1 + " " + bestIndex2 + " " + bestIndex3 + " "
+ bestIndex4);
out.println("Best Estimate = " + bestEstimate);
out.print("Relative error = ");
out.println(error);
in.close();
out.close();
}
}