简体   繁体   中英

How to calculate average of array input?

I've just recently started programming Java and I'm having a problem that's making me want to break things. It starting to get annoying, and I'm feeling pretty stupid right now.

The task is to write a program that asks for five numbers to be input into an array (yes, can't use a list) and to then calculate the average of the five numbers input.

Where am I going wrong?

My current code calculates the average after each input. I want to do that after they've all been inserted, otherwise what is the point?

All help is greatly appreciated, you believe me!

Here's the code:

import java.util.Scanner;

public class Uppg3 {
public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    int[] numbers = new int[5];

    int sum = 0;
    for (int i = 0; i < numbers.length; i++)
    {
        System.out.println("enter a number: ");
        numbers[i] = input.nextInt();
        sum = numbers[i];

    }

    double average = sum / 5;

    System.out.println("Average is " + average);
    input.close();
}
}

You ought to encapsulate such a thing in a method.

You can either process all the numbers at once by storing them in an array and passing them at the same time or keep a running tally and return it on request.

public class StatisticsUtils {
    private double sum;
    private int numValues;

    public StatisticsUtils() {
        this.sum = 0.0;
        this.numValues = 0;
    }

    public void addValue(double value) {    
        this.sum += value;
        ++this.numValues;
    }

    public double getAverage() {
        double average = 0.0;
        if (this.numValues > 0) {
            average = this.sum/this.numValues;
        }
        return average;
    }

    public static double getAverage(double [] values) {
        double average = 0.0;
        if ((values != null)  && (values.length > 0)) {
            for (double value : values) {
                average += value;
            }
            average /= values.length;      
        }
        return average;
    }
}

Looking at this for loop

for (int i = 0; i < numbers.length; i++)
{
    System.out.println("enter a number: ");
    numbers[i] = input.nextInt();
    sum = numbers[i];

}

You are adding it to the array just fine, but you keep setting the sum to whatever they enter. For example, if I enter 5 3 2, the array will have 5 3 2 in it, but the sum will be set to 2. If I only entered 5 3, it would be set to 3.

One solution would be to use +=, that way it adds that number to whatever the current value is (which you start at 0). It's also important to note that when you divide two integers (sum and 5), it will not give you a decimal. This can be solved by using 5.0 or casting sum to a double.

Your code is currently just assigning a different value to sum on every iteration of the loop. You need to change sum = numbers[i]; to sum += numbers[i]; (equivalent to sum = sum + numbers[i]; ). You'll also want to change int sum = 0; to double sum = 0 so that your answer has decimals.

Ok, it works with the "+=", love you Chris Phelps. That and changing:

 int sum = 0;

to

double sum = 0;

To make the answer have a decimal. Thanks a lot you guys! A weight has been lifted of my chest :).

Code for calculating average of array input

package com.imedxs;

import java.util.Scanner;

public class Test {

public static void main(String args[])
{
    Scanner sc=new Scanner(System.in);

    System.out.println("for calculating average of array");
    int n=0;
    int b=sc.nextInt();
    int[] numbers = new int[b];

    int sum = 0;
    for (int i = 0; i < b; i++)
    {
        System.out.println("enter a number: ");
        numbers[i] = sc.nextInt();
        sum += numbers[i];
        n++;

    }

    double average = sum / n;

    System.out.println("Average is " + average);

   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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