0

Beginners Java Student here.. I'm trying to create a student grade average program that stops when it a sentinel value of '999' is added. Here is what I've got:

import  

javax.swing.JOptionPane;

public class average_grades
{
public static void main (String []args)
{
    int num_grades = 0;
    int total_grade = 0;
    int average_grade = 0;
    int student_grade = 0;
    while (student_grade != 999)
        {
            student_grade = Integer.parseInt(JOptionPane.showInputDialog(null, "What is the student's grade?"));
            if (student_grade < 0)
                {
                    JOptionPane.showMessageDialog(null,"Invalid Entry");
                }
            else
                {
                    num_grades++;
                    total_grade = total_grade + student_grade;
                    average_grade = total_grade/num_grades;
                }

        }
JOptionPane.showMessageDialog(null, "Average: " + average_grade);
}
}

The problem I have is that the 999 is being added into the averages and I can't figure out what the simple solution is. Thanks!

5 Answers 5

3

Your issue is that the code in the else statement (which adds the value to the averages) is being run before the check of whether the value is the sentinel.

A simple fix could be:

while (true) {
    student_grade = Integer.parseInt(JOptionPane.showInputDialog(null, "What is the student's grade?"));
    if (student_grade == 999) break;
    else if (student_grade < 0) { /* your code */ }
    else { /* your code */ }
}

There are other ways to achieve this, too, but the order of what needs to happen:

  • get value for student grade entered by user
  • check if sentinel value; if so, break
  • check if value is valid; if not, repeat steps 2-3
  • add value to average calculation

EDIT: to solve this with the constraint that break cannot be used (per @baash05's request in the comments), do the following:

student_grade = Integer.parseInt( /* your code to get entry */ );
while (student_grade != 999) {
    if (student_grade < 0) { /* your code */ }
    else { /* your code */ }
    student_grade = Integer.parseInt( /* your code to get entry */ );
}

This code doesn't use break statements but still fulfills the correct order of what needs to happen.

Sign up to request clarification or add additional context in comments.

3 Comments

Nice and simple. But for a class/homework it breaches structured programming because it has the break, and the test for while exit should be in the loop control.
just get a boolean variable outside the loop thats set to true and used in the while's condition,and inside the condition of the 999 set the boolean to false.
@baash05 added an example to move the test for while exit to the loop control
1

This is not a Java problem. It is rather a repetition problem using while. You could base your logic on the following pseudo-code:

read grade;
while (grade != 999) {
   if (grade < 0) 
      println("Invalid Entry");
   else {
      num_grades++;
      total_grade = total_grade + grade;
      average_grade = total_grade/num_grades;
   }
   read grade;
}

print(average_grade);

Here is your program based on the p-code above:

import javax.swing.JOptionPane;

public class AverageGrades {
    public static void main(String[] args) {
        int num_grades = 0;
        int total_grade = 0;
        int average_grade = 0;
        int student_grade;
        student_grade = Integer.parseInt(JOptionPane.showInputDialog(null, "What is the student's grade?"));
        while (student_grade != 999) {        
            if (student_grade < 0) {
                JOptionPane.showMessageDialog(null, "Invalid Entry");
            } else {
                num_grades++;
                total_grade = total_grade + student_grade;
                average_grade = total_grade / num_grades;
            }
            student_grade = Integer.parseInt(JOptionPane.showInputDialog(null, "What is the student's grade?"));
        }
        JOptionPane.showMessageDialog(null, "Average: " + average_grade);
    }
}

Comments

0

Don't perform the calculations on the sentinel value. Add a condition to your else to stop that from happening.

else if (student_grade != 999)

Also, consider defining a constant for this sentinel value. In the class:

public static final int END = 999;

and later:

while (student_grade != END)

and

else if (student_grade != END)

Comments

0

A simple solution is to add a if condition in your else condition. You can find the change in the following code:

    import javax.swing.JOptionPane;

public class average_grades
{
public static void main (String []args)
{
    int num_grades = 0;
    int total_grade = 0;
    int average_grade = 0;
    int student_grade = 0;
    while (student_grade != 999)
        {
            student_grade = Integer.parseInt(JOptionPane.showInputDialog(null, "What is the student's grade?"));
            if (student_grade < 0)
                {
                    JOptionPane.showMessageDialog(null,"Invalid Entry");
                }
            else
                {
                    if(student_grade != 999)
                    {
                    num_grades++;
                    total_grade = total_grade + student_grade;
                    average_grade = total_grade/num_grades;
                    }
                    else
                    {
                        break;
                    }

                }

        }
JOptionPane.showMessageDialog(null, "Average: " + average_grade);
}
}

Comments

0

so very close mate

public static void main (String []args)
{
    int num_grades = 0;
    int total_grade = 0;
    int average_grade = 0;
    int student_grade;
    while (999 != (student_grade = Integer.parseInt(JOptionPane.showInputDialog(null, "What is the student's grade?"))  ))
        {

            if (student_grade < 0)
                {
                    JOptionPane.showMessageDialog(null,"Invalid Entry");
                }
            else
                {
                    num_grades++;
                    total_grade = total_grade + student_grade;
                    average_grade = total_grade/num_grades;
                }

        }
JOptionPane.showMessageDialog(null, "Average: " + average_grade);
}

I just moved the request into the while condition. It will test the value entered before running the body of the while

Others have pointed out some small things you could fix. Like the 999 as a constant. good idea. magic numbers (a raw number in the code) are most often a bad idea. Consider what 999 would mean to the psychopath that will eventually maintain your code. He'll know where you live and hunt you down, if you don't make his job easy.

Also checkout out Yoda conditions. If you're new and get in the habit it will save you heaps of pain.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.