J.doe
J.doe

Reputation: 31

How to calculate sum in java?

I need to write a code where you insert 10 grades and get back the average of those ten grades. I know how to do it, except I don't know how to calculate the sum of all the grades. I found on this site this code:

public int sumAll(int... nums) { //var-args to let the caller pass an arbitrary number of int

int sum = 0; //start with 0

    for(int n : nums) { //this won't execute if no argument is passed
        sum += n; // this will repeat for all the arguments
    }
    return sum; //return the sum
} 

so I wrote my code like this and it worked!:

import java.util.Scanner;

public class Loop7 {

    public static void main(String[] args) {

        Scanner scan = new Scanner (System.in);
        System.out.println("Please enter how many grades you want to insert : ");
        int num1 = scan.nextInt();
        int num;
        double sum =0;
        for(int i= 0; i<num1; i++)
        {
            System.out.println("Please enter a grade: ");
            num = scan.nextInt();
            sum += num;
        }
        System.out.println("the average is: "+(sum)/num1);

    }

so my question is what sum+=num; mean? how does that line give me the sum? and why I had to write double sum= 0?

Upvotes: 1

Views: 20540

Answers (3)

Adan Vivero
Adan Vivero

Reputation: 422

The reason you needed to write

double sum = 0.0; 

is because you needed to initialize the sum first. Next the

sum += num;

means

sum = sum + num;

just in a more simple way.

Upvotes: 0

Andres Alvarez
Andres Alvarez

Reputation: 162

Over here I explain each of those lines to better help you understand this code.

public class Loop7 {

    public static void main(String[] args) {

        Scanner scan = new Scanner (System.in); //this is what allows the user to input their data from the console screen
        System.out.println("Please enter how many grades you want to insert : "); //this just outputs a message onto the console screen
        int num1 = scan.nextInt(); //This is the total number of grades the user provides and is saved in the variable named num1
        int num; //just a variable with nothing in it (null)
        double sum =0; //this variable is to hold the total sum of all those grades
        for(int i= 0; i<num1; i++) //loops as many times as num1 (if num1 is 3 then it loops 3 times)
        {
            System.out.println("Please enter a grade: "); //output message
            num = scan.nextInt(); //every time the loop body executes it reads in a number and saves it in the variable num
            sum += num; //num is then added onto sum (sum starts at 0 but when you add 3 sum is now 3 then next time when you add 1 sum is now 4 and so on)
        }
        System.out.println("the average is: "+(sum)/num1); //to get the average of a bunch of numbers you must add all of them together (which is what the loop is doing) and then you divide it by the number of items (which is what is being done here)  

    }

Upvotes: 1

Alexander Mladzhov
Alexander Mladzhov

Reputation: 595

sum += num; Means that your sum which is declared 0 is added with num which you get from the console. So you simply make this: sum=sum+num; for the cycle. For example sum is 0, then you add 5 and it becomes sum=0+5, then you add 6 and it becomes sum = 5 + 6 and so on. And it is double because you are using division.

Upvotes: 0

Related Questions