Hi Netizens, I wish you guys can guide me in here. In the following code, I've compiled flawlessly and I didn't managed to get the output screen and I'm kind of in the dark since I didn't find the answer that I wanted through internet browsing after a while. Any reply will be appreciated :)

On the other side of the note, I managed to get the output with 3 variable in the showMessageDialog [output] but it disappear when I tried to add 'comment' variable inside.

//Enter the input for the marks and displays the corresponding grade using GUI

import javax.swing.JOptionPane;
import javax.swing.JFrame;
 
public class MarksGradeWithGUI
{
public static void main(String[] args)
{
    int testScore;
    int stu_id;
    String name1;
    String stu_matrix;
    String input;
    String stu_name;
    String grade;
    String comment;
    

    //Get the student name.
    name1 = JOptionPane.showInputDialog (null, "Please enter your name : ", "Your name", JOptionPane.QUESTION_MESSAGE);

    //Obtain the student matrix number.
    stu_matrix = JOptionPane.showInputDialog (null, "Please enter your 6-digit ID number: ", "Your Matrix Number", JOptionPane.QUESTION_MESSAGE);
                 
    //Get the numeric test score.
    input = JOptionPane.showInputDialog (null, "Enter your numeric test score and I will tell you the grade: ", "Your score", JOptionPane.QUESTION_MESSAGE);

            
    stu_name = (name1);

    stu_id = Integer.parseInt (stu_matrix);

    testScore = Integer.parseInt (input);

    grade = null;

    comment = "Try again for a better result!";

    //Display the grade.
    if (testScore < 40)
    {    
    grade = "F"; 
    comment = "Don't be sad! Try again!";    
    }
    else
        {
        if (testScore < 50)
        {
         grade = "D"; 
         comment = "Please put more effort!";
        }
        else
        {
            if (testScore < 65)
            {
            grade = "C";
            comment = "Be more hardworking and you'll strive!";
            }
                else
                {
                if (testScore < 80)
                {
                grade = "B";
                comment = "You're doing great! Just a little more for an A!";
                }
                    else
                    {
                    if (testScore < 101)
                    {
                    grade = "A";
                    comment = "Congratulations!";
                    }
                         else
                         {
                         JOptionPane.showMessageDialog(null, "Invalid entry.\n" + 
                         "Please enter your test score between (0-100)");
                         }

                String output = "Name : " + stu_name + "\nMatrix ID : 0" + stu_id + "\nGrade : " + grade + "\nComment : " + comment;

                JOptionPane.showMessageDialog (null, output, "Output Screen", JOptionPane.INFORMATION_MESSAGE);

                   }
                }
            }
        }
     
    System.exit(0);
}
}

Recommended Answers

All 5 Replies

Looks like the final display screen is deeply buried in the nested if tests when maybe it needs to be after all those - without proper indentation it's hard to tell

Sorry for the bad indentation, I've fixed it a little. It looks different when I copied it directly from notepad.

Still looks to me like the output screen is buried deep in the if/elses. Leaving aside the attempt to re-enter a bad score around line 76 (which needs more work), it should look more like

if (testScore < 40) {
   ...
} else {
   if  (testScore < 50) {
      ...
         etc 
            etc 
               etc
         }
      }
   }
}
// show output window - outside all those nested ifs

I've gotten the output once I took the output window outside from the nested ifs.. thanks :D!

Mark thread as solved?

Be a part of the DaniWeb community

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