dimanche 18 février 2018

Java terminating before running code after nested if statement

I am a student 1 month into comp-sci 1 so my knowledge is in the slim to none category. Below is my code for a program that takes the user input of 3 test scores and finds the average and tells the user the grade. The program compiles with no errors however after the if statement verifying that the 3rd score entered is in the valid range the program terminates without proceeding to the following block of nested if else statements. I've searched but can't figure out the problem, hoping to find help here from you fine folks, thanks.

grade1_string = JOptionPane.showInputDialog("Enter the first test score.");
grade1 = Integer.parseInt(grade1_string);

if (grade1 >= 0 && grade1 <= 110)//allowing for 10 pts extra credit
{
  grade2_string = JOptionPane.showInputDialog("Enter the second test score.");
  grade2 = Integer.parseInt(grade2_string);

  if (grade2 >= 0 && grade2 <= 110)
  {
     grade3_string = JOptionPane.showInputDialog("Enter the third test score.");
     grade3 = Integer.parseInt(grade3_string);

     if (grade3 >= 0 && grade3 <= 110)
     { 
        JOptionPane.showMessageDialog(null,"calcuating...");

        avrgGrade = (float)(grade1 * grade2 * grade3) / 3;

        if (avrgGrade >= 0 && avrgGrade < 60)
        {
           JOptionPane.showMessageDialog(null,"Sorry, your average is " + avrgGrade +
                                              ", an F");
        }
        else if (avrgGrade >= 60 && avrgGrade < 70)
        {
           JOptionPane.showMessageDialog(null,"Sorry, your average is " + avrgGrade +
                                              ", a D");
        }
        else if (avrgGrade >= 70 && avrgGrade < 80)
        {
           JOptionPane.showMessageDialog(null,"Not bad, your average is " + avrgGrade +
                                              ", a C");
        }
        else if (avrgGrade >= 80 && avrgGrade < 90)
        {
           JOptionPane.showMessageDialog(null,"Pretty good, your average is " + avrgGrade +
                                              ", a B");
        }
        else if (avrgGrade >= 90 && avrgGrade <= 110)
        {
           JOptionPane.showMessageDialog(null,"Way to go, your average is " + avrgGrade +
                                              ", an A");
        }
     }
     else
     {
        JOptionPane.showMessageDialog(null,"That is not a valid test score, " +
                                           "please run the program again to " +
                                           "calculate your average.");
     }  
  }
  else 
  {
     JOptionPane.showMessageDialog(null,"That is not a valid test score, please " +
                                        "run the program again to calculate your " +
                                        "average.");
  }   

At the end is a 3rd else with the same message about not entering a valid score(couldn't get the formatting right)

Aucun commentaire:

Enregistrer un commentaire