Home > Software design >  Calculate the average for user's input
Calculate the average for user's input

Time:02-17

I want to calculate the average for the user's input but I am not sure how to add up the grades since its a while loop and the user can keep entering grades. I also don't want to count the zero to exit the loop as part of the average.

public static void main(String[] args){
        
    Scanner input = new Scanner( System.in );
    {
        int grade = 0;
        int counter = 0;
        
        
        System.out.print("Enter grade or zero to quit(1-100):");
        grade= input.nextInt();
        
               
        while(grade>0)
        { 
            if (grade >= 90)
            System.out.println("A");
            

            else if (grade >= 80)
                    
            System.out.println("B");
            

            else if (grade >= 70)
            System.out.println("C");
            

            else if (grade >= 60)
            System.out.println("D");

            else
                {System.out.println("F");
                    }
            counter =1;
                    
            System.out.print("Enter grade or zero to quit(1-100):");
            grade=input.nextInt();
            
           
        }

ouput

I want to print one more line saying "The average of these (number of grades) Grades is(average)"

CodePudding user response:

Just keep a sum of all the grades, and divide by the count.

public static void main(String[] args){
    
    Scanner input = new Scanner( System.in );
    int grade = 0;
    int counter = 0;
    int sum = 0;
    
    while(grade>0) { 
    
        System.out.print("Enter grade or zero to quit(1-100):");
        grade= input.nextInt();
        sum  = grade;
        counter  ;
        float average = sum / counter;
        
        if (grade >= 90) {
            System.out.println("A");
        }
        else if (grade >= 80) {
            System.out.println("B");
        }
        else if (grade >= 70) {
            System.out.println("C");
        }
        else if (grade >= 60) {
            System.out.println("D");
        }
        else {
            System.out.println("F");
        }
    }
}

CodePudding user response:

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    int grade = 0;
    int counter = 1;
    int sum = 0;
    float average = 0;
    System.out.print("Enter grade or zero to quit(1-100):");
    grade = input.nextInt();
    if (grade > 0) {
        counter = 1;
        while (grade > 0) {
            System.out.print("Enter grade or zero to quit(1-100):");
            grade = input.nextInt();
            sum  = grade;
            counter  ;
            average = sum / counter;

            if (grade >= 90) {
                System.out.println("A");
            } else if (grade >= 80) {
                System.out.println("B");
            } else if (grade >= 70) {
                System.out.println("C");
            } else if (grade >= 60) {
                System.out.println("D");
            } else {
                System.out.println("F");
            }
        }
    } 
    System.out.println(average);
}

CodePudding user response:

public static void main(String[] args){
    
    Scanner input = new Scanner( System.in );
    int grade = 0;
    int counter = 0;
    int sum = 0;
    float average=0;
    do{
    System.out.print("Enter grade or zero to quit(1-100):");
        grade= input.nextInt();
        sum  = grade;
        counter  ;
       average = sum / counter;
        
        if (grade >= 90) {
            System.out.println("A");
        }
        else if (grade >= 80) {
            System.out.println("B");
        }
        else if (grade >= 70) {
            System.out.println("C");
        }
        else if (grade >= 60) {
            System.out.println("D");
        }
        else {
            System.out.println("F");
        }
    }while( grade > 0 );
    
        System.out.println(average);
    }
  • Related