简体   繁体   English

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

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

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. 我试图得到一个平方根的(9)数字数组,然后打印出来,但我一直只返回一个结果-数组平方的数字数量-显然不是我想要的。 Thanks for any help. 谢谢你的帮助。 Ok, here is my terrible code so far. 好的,到目前为止,这是我糟糕的代码。 Trying to pass it to a method as well. 也尝试将其传递给方法。

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;
 }
}

You have only a single variable result to store the square roots, so it gets overwritten and in the end it contains only the latest square root. 您只有一个变量result可存储平方根,因此它将被覆盖,最后只包含最新的平方根。 In case you want the square root of each element within the array, you need to store the results in an array as well, eg 如果您想要数组中每个元素的平方根,则还需要将结果存储在数组中,例如

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, eg 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

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. 遍历所有数组后,返回result的值,在这种情况下,它将是您处理的最后一个数字的平方根。

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. 确保您也支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM