为什么我的java代码没有得到正确的平均值?

时间:2022-09-04 08:06:43

So I am working on an assignment and pretty much its working with methods and returning. My code so far is supposed to print the numbers in an array then it takes those numbers and averages them. I have to use the methods I am given in the exact format they are given but I am not sure why the average is coming out wrong. It is printing 521674.2 as the average which is clearly wrong. Here is my code:

所以我正在完成一项任务,并且几乎使用方法并返回。到目前为止我的代码应该在数组中打印数字然后它取这些数字并对它们进行平均。我必须按照给出的确切格式使用我给出的方法,但我不确定为什么平均值出错了。打印521674.2作为平均值显然是错误的。这是我的代码:

public class Statistics
{

    public static void main(String[] args)
    {
        int[] nums = new int[]{5, 2, 1, 6, 7};

        printArray(nums);
        average(nums);
        double store = average(nums);
        System.out.println(store);
    }


    public static void printArray(int[] nums) {
        for(int i = 0; i < nums.length; i++){ 
            System.out.print(nums[i]); 
            }
    }


    public static double average(int[] values) {
        int sum = 0;
        double average;

        for(int i=0; i < values.length; i++){
            sum = sum + values[i];
        }
        average = (double)sum/values.length;
        return average;   

4 个解决方案

#1


5  

Actually, it printed the array (52167) and then the average (4.2).

实际上,它打印了阵列(52167),然后是平均值(4.2)。

To improve this, you may want to have a System.out.println(); at the very end of printArray, so that the output is on its own line.

要改进这一点,您可能希望拥有一个System.out.println();在printArray的最后,以便输出在它自己的行上。

You may also want to have a separator, like ,, so that it prints 5,2,1,6,7. For example:

你可能也想要一个分隔符,比如,它打印5,2,1,6,7。例如:

public static void printArray(int[] nums) {
    for (int i = 0; i < nums.length - 1; i++) { 
        System.out.print(nums[i]);
        System.out.print(","); 
    }
    if (nums.length > 0) { // make sure array isn't empty
        System.out.println(nums[nums.length - 1]);
    }
}

#2


2  

There's nothing wrong with your code. You're getting confused due to the output.

您的代码没有任何问题。由于输出,你会感到困惑。

If you comment the line that deals with printArray(), or change its System.out.print() to System.out.println(), then you'll see the results you expect.

如果您注释处理printArray()的行,或将其System.out.print()更改为System.out.println(),那么您将看到您期望的结果。

#3


1  

Currently your code is printing the contents of the array AND the average on the same line with no space in between. Remove the printArray() call and you can see that it will output only the average 4.2, which is correct:

目前,您的代码正在打印数组的内容和平均值在同一行上,两者之间没有空格。删除printArray()调用,您可以看到它只输出平均值4.2,这是正确的:

public static void main(String[] args) {
    int[] nums = new int[] { 5, 2, 1, 6, 7 };

    //printArray(nums);
    average(nums);
    double store = average(nums);
    System.out.println(store);
}

#4


0  

You should use .println() istead of print() then it will be fine

你应该使用.println()而不是print()然后就可以了

#1


5  

Actually, it printed the array (52167) and then the average (4.2).

实际上,它打印了阵列(52167),然后是平均值(4.2)。

To improve this, you may want to have a System.out.println(); at the very end of printArray, so that the output is on its own line.

要改进这一点,您可能希望拥有一个System.out.println();在printArray的最后,以便输出在它自己的行上。

You may also want to have a separator, like ,, so that it prints 5,2,1,6,7. For example:

你可能也想要一个分隔符,比如,它打印5,2,1,6,7。例如:

public static void printArray(int[] nums) {
    for (int i = 0; i < nums.length - 1; i++) { 
        System.out.print(nums[i]);
        System.out.print(","); 
    }
    if (nums.length > 0) { // make sure array isn't empty
        System.out.println(nums[nums.length - 1]);
    }
}

#2


2  

There's nothing wrong with your code. You're getting confused due to the output.

您的代码没有任何问题。由于输出,你会感到困惑。

If you comment the line that deals with printArray(), or change its System.out.print() to System.out.println(), then you'll see the results you expect.

如果您注释处理printArray()的行,或将其System.out.print()更改为System.out.println(),那么您将看到您期望的结果。

#3


1  

Currently your code is printing the contents of the array AND the average on the same line with no space in between. Remove the printArray() call and you can see that it will output only the average 4.2, which is correct:

目前,您的代码正在打印数组的内容和平均值在同一行上,两者之间没有空格。删除printArray()调用,您可以看到它只输出平均值4.2,这是正确的:

public static void main(String[] args) {
    int[] nums = new int[] { 5, 2, 1, 6, 7 };

    //printArray(nums);
    average(nums);
    double store = average(nums);
    System.out.println(store);
}

#4


0  

You should use .println() istead of print() then it will be fine

你应该使用.println()而不是print()然后就可以了