Java中的Math.pow和Math.sqrt

Java中的Math.pow和Math.sqrt,java,arrays,math.sqrt,Java,Arrays,Math.sqrt,好的,我发现了两个短代码,但我想了解它们是如何工作的。我在谷歌上搜索并查看了以下链接: 但其中的解释并不清楚。换句话说,我希望知道/理解这两个代码的每一行都发生了什么 A下面的程序片段计算给定的整数数组数据,并打印数据中所有条目的几何平均值: double product = 1;// For example, I understand why it is 1, since if it was 0 then the product would be keep getting 0, si

好的,我发现了两个短代码,但我想了解它们是如何工作的。我在谷歌上搜索并查看了以下链接:

但其中的解释并不清楚。换句话说,我希望知道/理解这两个代码的每一行都发生了什么

A下面的程序片段计算给定的整数数组数据,并打印数据中所有条目的几何平均值:

   double product = 1;// For example, I understand why it is 1, since if it was 0 then the product would be keep getting 0, since any number *0 is always zero.
    for(int i=0; i<data.length; i++)//okay for loop is getting out all the intergers from the data array.
    product*=data[i];//This is I am not too sure, I guess each item in array is getting multiplied with each other????
    double gmean=Math.pow(product,1.0/data.length); // Yes, I hate this line, because I don't understand it, can someone explain this line please? Please use easy English, I am not as smart as you.
double sum=0; //Okay the sum should be 0 because at the moment nothing has been summed up.
for(int i=0; i<data.length; i++)// Now getting out all the items in array called data.
sum+= data[i]*data[i];//Now I am not too sure, all the items in the array called data is getting multiplied with each other and then getting added up? I am not too sure, if would be good if someone could explain this with easy English.
double qmean = Math.sqrt(sum/data.length);// I hate this line, because I don't understand it.
System.out.println(qmean);// Displays the final result.
B第二个代码段计算给定的整数数组数据,并打印数据中所有条目的二次平均值:

   double product = 1;// For example, I understand why it is 1, since if it was 0 then the product would be keep getting 0, since any number *0 is always zero.
    for(int i=0; i<data.length; i++)//okay for loop is getting out all the intergers from the data array.
    product*=data[i];//This is I am not too sure, I guess each item in array is getting multiplied with each other????
    double gmean=Math.pow(product,1.0/data.length); // Yes, I hate this line, because I don't understand it, can someone explain this line please? Please use easy English, I am not as smart as you.
double sum=0; //Okay the sum should be 0 because at the moment nothing has been summed up.
for(int i=0; i<data.length; i++)// Now getting out all the items in array called data.
sum+= data[i]*data[i];//Now I am not too sure, all the items in the array called data is getting multiplied with each other and then getting added up? I am not too sure, if would be good if someone could explain this with easy English.
double qmean = Math.sqrt(sum/data.length);// I hate this line, because I don't understand it.
System.out.println(qmean);// Displays the final result.
好的,正如你所看到的,我确实理解代码中的一些行,但也有一些行,我不理解,如果有人能解释这些行,那就太棒了,我没有用简单的英语理解,也没有用复杂的方式


提前谢谢。

数学。powa,b表示a^b,而math.sqrta是a的平方根。您不了解内容中的java方法或数学逻辑?

假设数据为{3,2,7}。数据长度为3。这将计算sqrt9+4+49/3

double sum=0;
// sum is now zero

for(int i=0; i<data.length; i++)
// Execute the following statement with i having each value starting from 0,
// incrementing by 1 each time (i++), as long as i remains less than 3.
    sum+= data[i]*data[i];
    // The sum+= statement is executed three times, with i each of 0, 1, and 2.
    // The first time adds 9 to sum getting 9
    // The second time adds 4 to sum getting 13
    // The third time adds 49 to sum, getting 62

double qmean = Math.sqrt(sum/data.length);
// make qmean equal to sqrt(62/3).

System.out.println(qmean);// Displays the final result.

这只是简单的数学,将和RMS的公式转换为Java代码。您已经承认第一个代码确定几何平均值,第二个确定二次平均值。@NullUserException,谢谢您的回答,但您知道,我没有您那么聪明,虽然您觉得简单,但我觉得非常困难,我很难理解代码中的某些行,这就是为什么我会善意地询问是否有人能够逐一解释我不理解的行,因为这是唯一能帮助我的事情。@Acemi,我不是有意屈尊;但是,如果您知道平均值的公式,那么理解代码应该非常简单。您看过Math api吗?这些方法实际上没有做任何名称在内容中没有告诉你的事情,只是:double gmean=Math.powproduct,1.0/data.length;=product^1/data.length它只是一个数学java代码,表示一个数字的有效性,double qmean=Math.sqrtsum/data.length;是sum的data.length平方根吗?我们可以这么说,这些代码实际上就像烹饪食谱?另外,如果我想检查数据[]={1,2,3}这3个整数的两个代码,我该如何修改这两个代码?请注意,这个答案中的2^3表示2,其中3为上标。^也是Java的异或运算符,但这里不是这样使用的。