数组 - 数组的平方根并打印结果 JAVA

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2945613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:36:16  来源:igfitidea点击:

Arrays- Square root of an Array and printing the result JAVA

javaarrayssquare-root

提问by roger34

I'm trying to get an array of (9) numbers square rooted then printed but I keep coming back with only one result - the number of numbers in the array squared- obviously not what I want. Thanks for any help. Ok, here is my terrible code so far. Trying to pass it to a method as well.

我试图得到一个 (9) 数字的平方根数组然后打印,但我一直只返回一个结果 - 数组中数字的平方数 - 显然不是我想要的。谢谢你的帮助。好的,到目前为止,这是我糟糕的代码。也试图将它传递给一个方法。

public static void main ( String args[] )
{ 
 double[] nums  = {126, 12.939, 795, 320.16,
             110, 34.7676, 7773, 67, 567, 323};

System.out.println ("Square root is " +square);
square(nums);
} 

public static double square (double [] array) {
double result;
for( double i = 0; i < array.length ; i++ )
  result = Math.sqrt(array[i]);

return result;
 }
}

回答by Péter T?r?k

You have only a single variable resultto store the square roots, so it gets overwritten and in the end it contains only the latest square root. In case you want the square root of each elementwithin the array, you need to store the results in an array as well, e.g.

你只有一个变量result来存储平方根,所以它会被覆盖,最后它只包含最新的平方根。如果您想要数组中每个元素的平方根,则还需要将结果存储在数组中,例如

public static double[] square (double [] array) {
  double[] result = new double[array.length];
  for(int i = 0; i < array.length ; i++ )
    result[i] = Math.sqrt(array[i]);

  return result;
}

Then you can print out the results one by one, e.g. like this:

然后你可以将结果一一打印出来,例如像这样:

public static void main ( String args[] )
{ 
  double[] nums  = {126, 12.939, 795, 320.16,
             110, 34.7676, 7773, 67, 567, 323};
  double[] squares = square(nums);

  for(int i = 0; i < nums.length ; i++ )
    System.out.println ("Square root of " + nums[i] + " is " + squares[i]);
}

Update:and the result on my machine, as expected, is:

更新:正如预期的那样,我机器上的结果是:

Square root of 126.0 is 11.224972160321824
Square root of 12.939 is 3.597082150855051
Square root of 795.0 is 28.19574435974337
Square root of 320.16 is 17.893015397076034
Square root of 110.0 is 10.488088481701515
Square root of 34.7676 is 5.896405684821898
Square root of 7773.0 is 88.16461875378354
Square root of 67.0 is 8.18535277187245
Square root of 567.0 is 23.811761799581316
Square root of 323.0 is 17.97220075561143

回答by npinti

If you want to print the square root of all the elements in the array, you have to iterate and print the results in the same method. Something like:

如果要打印数组中所有元素的平方根,则必须以相同的方法迭代并打印结果。就像是:

for (int i = 0 ;i < array.length; i++)
{
    System.out.println("The Square Root of " + array[i] + " is" + Math.sqrt(array[i]));
}

The way you are doing is as follows: For each number in your array, find its square root and store it in variable result. Once that you have looped through all the array, return the value of result, which in this case will be the square root of the last number you have processed.

你这样做的方法如下:对于数组中的每个数字,找到它的平方根并将其存储在变量结果中。遍历完所有数组后,返回结果的值,在这种情况下,它将是您处理的最后一个数字的平方根。

回答by The Real Diel

In addition to what has been said...it appears you are calling the function after you are printing the answer. Make sure you shore that up too.

除了已经说过的内容之外……您似乎在打印答案后正在调用该函数。确保你也支持它。