简体   繁体   中英

How to extract numbers from an array list, add them up and divide by the amount of numbers in the array list?

I was watching a lesson online about arrays. It taught me the 'basics' of array lists: how to create an array list. So I was wondering, how would one go about creating an array list from the users' input ( JOptionPane ), extract the numbers out of it, add them up and divide by the total amount of numbers in the array list (long story short, calculate the average of the array)?

Here's my, somewhat of an approach:

import java.util.Arrays;
import javax.swing.JOptionPane;
public class JOptionPaneTesting {

    public static void main(String[] args){
        int grades = Integer.parseInt(JOptionPane.showInputDialog("What are your grades of this month?"));
        int arrayList[] = {Integer.valueOf(grades)};
        int arraysum;
        arraysum = arrayListGetFirstIndex + arrayListGetSecondIndex + ...; //Extracts all of the indices and adds them up?
        int calculation;
        calculation = arraysum / arrayListAmmountOfNumbersInTheList; //An example of how it go about working
    }
}

As far as i understand the question, you are trying to get input from user. The input is the grades. Then you wanted to add up the grades and calculate the average of the grades.

public static double calculateAvg(List<Double>inputGrades){
        List<Double> grades = new ArrayList<Double>(inputGrades);
        double totalScore = 0.0;
        double avgScore = 0.0;
        if(grades !=null && grades.size()>0){
            for(Double grade : grades){
                totalScore = totalScore + grade;
            }
        avgScore = totalScore / grades.size();
        }

        return avgScore;
    }

Taking user input and adding it to the list

List<Double> gradesList= new ArrayList<Double>();
                gradesList.add(25.5);
                gradesList.add(29.5);
                gradesList.add(30.5);
                gradesList.add(35.5);
        System.out.println(calculateAvg(gradesList));

first create a list

List<Integer> list = new ArrayList<Integer>();`

then list.add(grade); you need iterate whole above line if you want add more than one grades. then list.get(index) get you specific (index) grade.

to calculate arraySum use :

for(int i = 0; i < list.size() ; i++)
    arraysum += list.get(i); // and others are same.

I hope this helps you!

This would be a suitable solution too:

String[] input = JOptionPane.showInputDialog("What are your grades of this month?").split(" ");
        double[] grades = new double[input.length];
        double average = 0.0;
        for (int i = 0; i < input.length; i++) {
            // Note that this is assuming valid input
            grades[i] = Double.parseDouble(input[i]);
            average+=grades[i];
        }
        average /= grades.length;

So you could type in multiple "grades" seperated by a whitespace.

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