Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 平方根猜测公式中的错误值_Java_Arrays_Algorithm_While Loop - Fatal编程技术网

Java 平方根猜测公式中的错误值

Java 平方根猜测公式中的错误值,java,arrays,algorithm,while-loop,Java,Arrays,Algorithm,While Loop,我有一个程序,要求用户输入任何数字(通常是一个大数字),并将其存储在变量“mu”中。然后它要求用户输入任意4个有效值。我试图创建的公式只需要取4个有效值,并将它们设置为w、x、y和z。我的公式就是这样。mu=w^a*x^b*y^c*z^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}。程序的要点是遍历每个可能的值,直到选择的每个指数都与μ最接近。例如,如果用户输入的μ为200000,而他对w、

我有一个程序,要求用户输入任何数字(通常是一个大数字),并将其存储在变量“mu”中。然后它要求用户输入任意4个有效值。我试图创建的公式只需要取4个有效值,并将它们设置为w、x、y和z。我的公式就是这样。mu=w^a*x^b*y^c*z^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}。程序的要点是遍历每个可能的值,直到选择的每个指数都与μ最接近。例如,如果用户输入的μ为200000,而他对w、x、y、z的输入为45180402110。那么程序的任务是猜测要设置为指数的值,以便45^a*180^b*402^c*110^d与μ或200000最接近。现在,我尝试使用while循环来实现这一点,但是我的程序似乎没有选择正确的猜测。(编辑并添加了另一个错误进行比较,但我认为它不正确)

以下是我目前的代码:

double[] Guess = { -5, -4, -3, -2, -1, -1.0 / 2, -1 / 3, -1.0 / 4.0, 0,
                1.0 / 4.0, 1.0 / 3.0, 1.0 / 2.0, 1, 2, 3, 4, 5 };
        int a = 0;
        int b = 0;
        int c = 0;
        int d = 0;
        double er = 0.0;


        out.print("Enter a value for mu:");
        double mu = in.nextDouble();
         double er1 = Math.abs((mu-mu)/mu);
        out.print("Enter your first favorite positive value:");
        double w = in.nextDouble();
        out.print("Enter your second favorite positive value:");
        double x = in.nextDouble();
        out.print("Enter your third favorite positive value:");
        double y = in.nextDouble();
        out.print("Enter your fourth favorite positive value:");
        double z = in.nextDouble();
        while (a < Guess.length) {
            double W = Math.pow(w, Guess[a]);
            while (b < Guess.length) {
                double X = Math.pow(x, Guess[b]);
                while (c < Guess.length) {
                    double Y = Math.pow(y, Guess[c]);
                    while (d < Guess.length) {
                        double Z = Math.pow(z, Guess[d]);
                        er = ((W * X * Y * Z) - mu) / mu; //calculates percent error
                        if (er < 0)
                        {
                         er = er1 * -1;
                        }
                        if (er < er1){
                          er1 = er;
                        }

                        d++;
                    }
                    d = 0;
                    c++;
                }
                c = 0;
                b++;
            }
            b = 0;
            a++;
        }

        out.print("error = " + er);
double[]Guess={-5,-4,-3,-2,-1,-1.0/2,-1/3,-1.0/4.0,0,
1.0 / 4.0, 1.0 / 3.0, 1.0 / 2.0, 1, 2, 3, 4, 5 };
int a=0;
int b=0;
int c=0;
int d=0;
双er=0.0;
打印(“输入mu:”的值);
double mu=in.nextDouble();
双er1=数学绝对值((μ)/μ);
打印(“输入您最喜欢的第一个正值:”);
双w=in.nextDouble();
打印(“输入您第二喜欢的正值:”);
双x=in.nextDouble();
打印(“输入您最喜欢的第三个正值:”);
双y=in.nextDouble();
打印(“输入您最喜欢的第四个正值:”);
双z=in.nextDouble();
while(a
任何人都知道我可能做错了什么,或者我需要补充什么?谢谢你的帮助!我知道我的while循环会给我正确的数字,但是我认为我没有正确设置我的a、b、c、d或我的错误百分比

编辑=我得到的错误值非常大。一个非常大的值。

检查这个

public static void main(String[] args) {
        double[] Guess = { -5, -4, -3, -2, -1, -1.0 / 2, -1 / 3, -1.0 / 4.0, 0, 1.0 / 4.0, 1.0 / 3.0, 1.0 / 2.0, 1, 2, 3, 4, 5 };
        int[] Result = {0,0,0,0};
        double e = 0.0;
        boolean first = true;
        double mu=200000, w=45,x=180,y=402,z=110;

        for(int a = 0; a<Guess.length; a++)
        {
            double W = Math.pow(w,Guess[a]);
            for(int b = 0; b<Guess.length; b++)
            {
                double X = Math.pow(x,Guess[b]);
                for(int c = 0; c<Guess.length; c++)
                {
                    double Y = Math.pow(y, Guess[c]);
                    for(int d = 0; d<Guess.length; d++)
                    {
                        double Z = Math.pow(z, Guess[d]);
                        double temp = Math.abs (W*X*Y*Z - mu);
                        if (first)
                        {
                            e = temp;
                            first = false;
                        }
                        else if (temp<e)
                        {
                            e=temp;
                            Result[0]=a;
                            Result[1]=b;
                            Result[2]=c;
                            Result[3]=d;
                        }
                    }
                }
            }
        }

        System.out.print("Error = " + e*100/mu + '\n');
        System.out.print("a=" + Guess[Result[0]] + "\nb=" + Guess[Result[1]] + "\nc=" + Guess[Result[2]] + "\nd=" + Guess[Result[3]] + '\n');
        }
publicstaticvoidmain(字符串[]args){
双[]猜测={5,-4,-3,-2,-1,-1.0/2,-1/3,-1.0/4.0,0,1.0/4.0,1.0/3.0,1.0/2.0,1,2,3,4,5};
int[]结果={0,0,0,0};
双e=0.0;
布尔值优先=真;
双μ=200000,w=45,x=180,y=402,z=110;

对于(int a=0;a1)使用for循环而不是while循环,这将整理你的代码。2)你需要存储错误并找到最小值的答案…我想你可能想用
for
循环重写它。这样你的逻辑会更明显。使用for循环会更容易是的,但我要求用该死的while循环重新编写。我只是cant在这里找到我的错误。你的问题是你没有在循环中执行任何操作。你试图强制执行最小值,因此需要最小化…0,我认为错误应该是
Math.abs()
-ed才能正确比较1,你没有比较错误,2,你没有存储具有最小错误的值。