Home > Software design >  Why is the average of 10 numbers always printed as 0.0?
Why is the average of 10 numbers always printed as 0.0?

Time:07-26

I've got this code which is meant to take 10 numbers from the keyboard and print their sum int sum and average double avg. The sum of the numbers is calculated and printed correctly however their average is always printed as 0.0. What's the cause? I'm getting no problems in my IDE

public class Main {
    public static void main(String[] args) {
        System.out.println("Input 10 numbers:");
        Scanner in = new Scanner(System.in);
        int sum = 0;
        double avg = sum / 10;
        for(int i = 1; i <= 10; i  ) {
            int num = in.nextInt();
            sum  = num;
        }
        System.out.println("The sum of your numbers is "   sum);
        System.out.println("The average of your numbers is "   avg);
    }
}

CodePudding user response:

You are only assigning a value to avg one time, when the sum is still zero. So, the computed average is zero.

Move the line, double avg = sum / 10 after the loop, when the sum has been tallied.

I wonder if you have some confusion about how expressions and assignments work. When you write avg = sum / 10, you aren't saying that avg will continuously update whenever sum changes; rather, avg will be computed once, using the value of sum at that time.

As commenters point out, to avoid rounding the average to an integer, you need to promote the operands to double. The most conspicuous way to do this would be to cast sum to double before dividing:

double avg = (double) sum / 10;

CodePudding user response:

The order of your statements doesn't make sense, and you divide without casting. You probably meant this:

import java.io.*;
import java.util.*;

public class Test2 {
    public static void main(String[] args) {
        System.out.println("Input 10 numbers:");
        Scanner in = new Scanner(System.in);
        int sum = 0;
        for(int i = 1; i <= 10; i  ) {
            int num = in.nextInt();
            sum  = num;
        }
        double avg = ((double)sum) / 10;

        System.out.println("The sum of your numbers is "   sum);
        System.out.println("The average of your numbers is "   avg);
    }
}

CodePudding user response:

Java isn't like math; just like in most programming languages, this statement:

double avg = sum / 10;

is not anything like what it would mean in a math paper. In a math paper, this is setting up an alias - it decrees that avg is defined as 'a tenth of the sum'. avg is always a tenth of the sum; if sum changes, so does avg.

In java, it does not work like that. double avg = sum / 10:

  • Decrees that a local var named avg now exists.
  • It is restricted to only contain double values.
  • It is assigned right now the result of executing, right now, sum / 10, where sum is itself a variable.

If you later change sum, it will not change avg.

Hence, your code creates a local var sum, makes it 0, and then assigns 0 / 10 to avg, which is, of course, 0 and will always be. You then modify sum but that's not going to modify avg.

In addition (because computer programs can, of course, have more than one error in there), in java x / y does not mean what you think it means: if the type of the lefthand expression and the type of the righthand expression are both integers, then the / means integer division. Meaning, for example, 5 / 3? That's 1. Not 1.666. Just '1': Integer division is: Do the division and just toss the stuff after the comma in the garbage (i.e. round down for positive numbers, round up for negative ones).

The fact that you then assign this to a double doesn't matter; the order: first do the (integer!) division, then convert the integer to a double - the fractional part is already tossed.

You want to force double division; easiest way to do that is probably to just declare sum as a double in the first place; if either left or righthand side is double, you get double division. Alternatively, you can first cast one of them to double, or start it all off with a 1.0 - many different ways to accomplish the same goal.

In short, make sum a double to ensure you get double division, and calculate the average after calculating the sum:

  • Move double avg = sum / 10 to after the for loop.
  • Make that double sum = 0;.

Then your code works fine.

  • Related