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.
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
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 element within the array, you need to store the results in an array as well, e.g.Then you can print out the results one by one, e.g. like this:
Update: and the result on my machine, as expected, is: