user3314727
user3314727

Reputation: 17

java Calculating the mean average

I am trying to do the following java assignment and every thing seems to work fine except when I put a number<4 or >10 it prints both "Invalid grade!" "You didn't enter any data!" what I wanted is to print only "Invalid grade!" I tried to fix it but I couldn't. Can any one help me please?

Thanks,

Assignment: Create a program that asks for results of exams and calculates the mean average of the grades. Grades are floating point numbers between 4 and 10. Program asks for grades until a negative number is typed. If user gives a grade other than a number between 4 and 10, the text “Invalid grade!” will be printed on screen and program asks for another grade. Finally the program prints the number of inputted grades and their mean average on screen as shown in the example print. If no grades were inputted, the notice “You did not input any grades.” is the only thing printed on screen.

Hint:A double type variable is to be used to store the value of the average.

Program is written to a class called Average.

Example output

Program calculates the average of inputted grades. Finish with a negative integer.

Input a grade (4-10): 5
Input a grade (4-10): 6,5
Input a grade (4-10): 7,5
Input a grade (4-10): 7
Input a grade (4-10): 0
Invalid grade!
Input a grade (4-10): -4

4 grades inputted.

Average of the grades: 6.5

Code

import java.util.Scanner;
public class apples {

   public static void main(String[] args) {

      int inputNumber=0;   
      int sum;           
      int count;         
      double average;    



      sum = 0;
      count = 0;


      Scanner reader = new Scanner(System.in);
      System.out.println("Program calculates the average of inputted grades.");
      System.out.println("Finish with a negative integer.");
      System.out.print("Input a grade (4-10): ");
      inputNumber = reader.nextInt();


      while (inputNumber > 0 && inputNumber>=4 && inputNumber<=10) {
         sum += inputNumber;   
         count++;              
         System.out.print("Input a grade (4-10): ");
         inputNumber = reader.nextInt();

      }

      if (inputNumber<4 || inputNumber>10) {
          System.out.println("Invalid grade!");
      }

      if (count==0) {
          System.out.print("You didn't enter any data!");
      }
      else {
         average = ((double)sum) / count;
         System.out.println();
         System.out.println(count + " grades inputted.");
         System.out.print("Average of the grades: "+ average);
      }

   } 

} 

Upvotes: 0

Views: 1477

Answers (5)

skutz
skutz

Reputation: 1

import java.util.Scanner;

public class Average {
    public static void main(String[] args) {
        int total = 0;
        double number_sum = 0, number_entered = 0, avg = 0;
        Scanner reader = new Scanner(System.in);
        System.out
                .print("Program calculates the average of inputted grades. \nFinish with a negative integer.\n\n");
        do {
            System.out.print("Input a grade (4-10): ");
            number_entered = reader.nextDouble();
            if (number_entered <= 10 && number_entered >= 4) {
                number_sum = number_sum + number_entered;
                total++;
            }
            if (number_entered == 0 || number_entered < 4
                    || number_entered > 10) {

                System.out.print("Invalid grade!");
                System.out.print("\nInput a grade (4-10): ");
                number_entered = reader.nextDouble();
            }
            if (number_entered < 0) {
                if (total == 0) {
                    System.out.print("You did not input any grades.");
                } else {
                    avg = number_sum / total;
                    System.out.print(total + " grades inputted.\n");
                    System.out.print("Average of the grades: " + avg);
                }
            }
        } while (number_entered > 0);
    }
}

Upvotes: -1

Dan Temple
Dan Temple

Reputation: 2764

Yep, an if - else will help here, but where you put it also matters.

    while (inputNumber > 0) {
        if (inputNumber<4 || inputNumber>10) {
            System.out.println("Invalid grade!");
        } else {
            sum += inputNumber;
            count++;
        }
        System.out.print("Input a grade (4-10): ");
        inputNumber = reader.nextInt();
    }

Upvotes: 0

Ben Pearson
Ben Pearson

Reputation: 7752

As this is an assignment, I'd prefer to give you a clue rather than the answer..

Take a close look at your while loop - it's never being entered. This means count is 0 when you input, for example, 3. As it's executing, you are meeting both conditions which means both lines are being printed. You'll need to modify the code to make sure that one or the other condition is met (or reached), but not both.

Upvotes: 0

Amit Dawle
Amit Dawle

Reputation: 13

As you finish with a negative integer the inputNumber will always be less than 4 so

if (inputNumber<4 || inputNumber>10) {
      System.out.println("Invalid grade!");
  }

is always true.

Change it to following (slightly easier to read):

 do {    
      System.out.print("Input a grade (4-10): ");
      inputNumber = reader.nextInt(); 
      if (inputNumber<4 || inputNumber>10) {
         break ; // 
      }
    sum += inputNumber;   
    count++;              


  }while (count <= 4) {

  if (count==0) {
      System.out.print("You didn't enter any data!");
  }

Upvotes: 0

Tim B
Tim B

Reputation: 41188

Put an else in before the second if, so it is only done if the first one was not:

if (inputNumber<4 || inputNumber>10) {
    System.out.println("Invalid grade!");
} else if (count==0) {
    System.out.print("You didn't enter any data!");
}

Upvotes: 3

Related Questions