user2984143
user2984143

Reputation: 85

Calculating the average number in a nesting loop Java

does anyone knows how to calculate the average in a loop. Every time I calculated the average I received 0 or 1. I know that I need use average = (sum) / (salary_annually); but I can't get it to work. Thanks in advance.

 import java.util.Scanner;

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

    int max = Integer.MIN_VALUE;
    int min = Integer.MAX_VALUE;
    int sum = 0;
    int average=0;
    int count = 0;
    int salary_annually = 0;

    for(int employee =1; employee <= 2; employee++)
    {
    System.out.println("Employee: " + employee);

         for(int year=1; year <= 2; year++)
         {

            System.out.println("Please Enter the Salary for Year: "  + year);
              salary_annually = kb.nextInt();

                          sum += salary_annually  ;
              if (min >= salary_annually) 
              { 
                     min = salary_annually;
              } 
             if (max <=salary_annually) 
             { 
               max = salary_annually;
              } 

            average = (sum) / (salary_annually); 


         }

          System.out.println("The average is " + average);
       System.out.println("The higher number  " + max);
       System.out.println("The the lowest number " + min);
     }

   }
  }

Upvotes: 1

Views: 1865

Answers (4)

flex36
flex36

Reputation: 146

The Average is calculated by average = sum / count where average should be of type double.

You did declare the variable count, but didn't use it.

import java.util.Scanner;

public class Calulate {

/**
 * @param args
 */
public static void main(String[] args) {
    Scanner kb = new Scanner(System.in);

    int max = Integer.MIN_VALUE;
    int min = Integer.MAX_VALUE;
    int sum = 0;
    double average = 0;
    int count = 2;
    int salary_annually = 0;

    for (int employee = 1; employee <= 2; employee++) {
        System.out.println("Employee: " + employee);

        for (int year = 1; year <= count; year++) {

            System.out.println("Please Enter the Salary for Year: " + year);
            salary_annually = kb.nextInt();
            sum += salary_annually;

            if (min >= salary_annually) {
                min = salary_annually;
            }
            if (max <= salary_annually) {
                max = salary_annually;
            }

        }

        average = sum / count; 

        System.out.println("The average is " + average);
        System.out.println("The higher number  " + max);
        System.out.println("The the lowest number " + min);
    }

}

}

Upvotes: 0

Leviathan
Leviathan

Reputation: 282

  1. average is calculated by: average = sum / count;
  2. you need to increment your count variable, otherwise you will always get and ArithmeticException / by zero

Upvotes: 0

johnsoe
johnsoe

Reputation: 674

I'm guessing the problem here is that you are using integer division. Since the sum and salary_annually are both integers division works slightly different. There is not remainder because dividing two integers gives an int.

For example 1/2 is not .5 as you might expect but instead it is 0. Any fractional number is dropped when doing integer math. As another example 9/5 is not 1.8 but instead 1.

If you want the average then you can either declare sum or salary_annually as a double and declare the average as a double as well.

Upvotes: 1

Mengjun
Mengjun

Reputation: 3197

Change

 average = (sum) / (salary_annually); 

To

 double average=0;// Declare it as `double` rather than `int`

 average = (sum) / 2.0; 

Upvotes: 0

Related Questions