New to Java and would appreciate the help with the assignment: Write a program to collect exam grades from keyboard until an invalid grade (grade less than 0 or bigger than 100) inputted. Find how many grades were entered, the corresponding average grade, how many grades above 90, and how many grades lower than 60
-I am able to input the first two number and then recieve an error.
Could someone please simply explain why this does not work properly? Thank you!

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
int grades;
int avg=0;
int totalCount=0;
int agrades=0;
int fgrades=0;
Scanner input = new Scanner(System.in);

System.out.println("Enter Grades: ");
grades = input.nextInt();
boolean numbers = true;
while (numbers){
    grades = input.nextInt();
    if (grades < 0 || grades > 100)
        numbers = false;

    avg = grades + grades/totalCount;
    totalCount++;
    if (grades >= 90)
        agrades++;
    if (60 >= grades)
            fgrades++;
    System.out.println("Enter Grades: ");
    grades = input.nextInt();
}
System.out.println("Amount of Grade: " + totalCount);
System.out.println("Averaage Grade: " + avg);
System.out.println("Number of grades above 90: " + agrades);
System.out.println("Number of grades below 60: " + fgrades);
    }
}

Recommended Answers

You should explain EXACTLY what "does not work properly" means... eg what error message do you get, or how does the output differ from what you expected.

Anyway - for the moment I notice that you have grades= nextInt(); in 3 places and after two of those you never …

Jump to Post

All 2 Replies

You should explain EXACTLY what "does not work properly" means... eg what error message do you get, or how does the output differ from what you expected.

Anyway - for the moment I notice that you have grades= nextInt(); in 3 places and after two of those you never use that value of grades. That means you are ignoring 2/3 of the ints in the file.
You also include the last (end of file) value in your calculations.
And your calculation of the average (a) will cause a zero divide on its first pass and (b) is wrong anyway.

Oh, that's exactly what I was looking for. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.