title: How to calculate sum in java?
body: <p>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:</p>
  
  <pre><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
  } 
  </code></pre>
  
  <p>so I wrote my code like this and it worked!: </p>
  
  <pre><code>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&lt;num1; i++)
          {
              System.out.println("Please enter a grade: ");
              num = scan.nextInt();
              sum += num;
          }
          System.out.println("the average is: "+(sum)/num1);
  
      }
  </code></pre>
  
  <p>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?</p>