Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Need some help

  1. #1
    Junior Member
    Join Date
    Dec 2021
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need some help

    I am working on a program that needs to allow a teacher to put in grades for an exam but I need it to ask for more then one student if the question of How many students? is greater then 1 then it needs to ask for the students first name, last name, and score,

    Then it needs to calculate the score and give it a letter grade

    This is my code so far

    import javax.swing.JOptionPane;


    public class Project
     
    {
     
    public static void main(String[] args)
     
    {
     
    double Score = 0;
     
    char LetterGrade = 0;
     
    String strMessage;
     
     
     
     
     
    JOptionPane.showInputDialog("Please enter the total possible points for the test");
     
     
     
    JOptionPane.showInputDialog("How many students do you have to enter");
     
     
     
    JOptionPane.showInputDialog("Please enter students first name");
     
     
     
    JOptionPane.showInputDialog("Please enter students last name");
     
     
     
    JOptionPane.showInputDialog("Please enter students test score");
     
     
     
     
     
    if(Score<=90)
     
    LetterGrade='A';
     
    else if (Score<=80)
     
    LetterGrade='B';
     
    else if (Score<=70)
     
    LetterGrade='C';
     
    else if (Score<=60)
     
    LetterGrade='D';
     
    else
     
    LetterGrade='F';
     
     
     
    strMessage=String.format("Your score of %.2f has earned you a letter grade of %c", Score,LetterGrade);
     
     
     
    JOptionPane.showMessageDialog(null, strMessage);
     
     
     
    }
     
     
     
    }
    Last edited by techs2211; December 9th, 2021 at 09:57 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need some help

    Sounds like you need a loop that iterates one time for each student.

    The assigning of letter grades code is in the wrong order. Most grades will be less the 90 and will get an A. Did you mean to test if the grade was greater than 90? Use > for greater

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2021
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need some help

    that is the problem I am having I am not sure how to write that I am new to java code
    Could you help me with that

    --- Update ---

    ok I was able to get the loop going but I am having problems with how to calculate the score this is the code now

     
    import javax.swing.JOptionPane;
     
    public class Project
    {
     
     
    public static void main(String[] args)
    {
    double Score = 0;
    char LetterGrade = 0;
    String strMessage;
     
     
    JOptionPane.showInputDialog("Please enter the total possible points for the test");
     
    int numStudents = Integer.parseInt(JOptionPane.showInputDialog("How many students do you have to enter"));
     
    for (int i = 0; i < numStudents; i++) {
        String name = JOptionPane.showInputDialog(null, "Enter Student Name: ");
     
    JOptionPane.showInputDialog("Please enter students last name");
     
    JOptionPane.showInputDialog("Please enter students test score");
     
     
    if(Score<=90)
    LetterGrade='A';
    else if (Score<=80)
    LetterGrade='B';
    else if (Score<=70)
    LetterGrade='C';
    else if (Score<=60)
    LetterGrade='D';
    else
    LetterGrade='F';
     
    strMessage=String.format("Your score of %.2f has earned you a letter grade of %c", Score,LetterGrade);
     
    JOptionPane.showMessageDialog(null, strMessage);
     
    }
     
    }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need some help

    having problems with how to calculate the score
    Please explain. What are you trying to do? Explain in some detail.
    What does the program currently do now? Copy its output and paste it here.

    The code is still testing for less than 90 (<) instead of greater than 90 (>)
    A score of 22 would be less than 90 and therefore would get a letter grade of A


    The posted code has lost all of its indentations which makes it harder to read an understand.
    Please fix the indentations of the code.

    The showInputDialog method returns a String that needs to be assigned to a variable so the program can use it:
    String someVar = JOptionPane.showInputDialog(....
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2021
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need some help

    ok here is my updated code
    What I am needing help with is when I enter the number of points for the exam and the students score I need it to display the score with the right Grade Letter

    import javax.swing.JOptionPane;
     
    public class Project
    {
     
     
    public static void main(String[] args)
    {
    	double points =0;
    	double score = 0;
    	char LetterGrade = 0;
    	String strMessage;
     
     
    	points=Double.parseDouble(JOptionPane.showInputDialog("Please enter the total possible points for the test"));
     
    	int numStudents = Integer.parseInt(JOptionPane.showInputDialog("How many students do you have to enter"));
     
    for (int i = 0; i < numStudents; i++) {
         JOptionPane.showInputDialog(null, "Enter Student Name: ");
     
         JOptionPane.showInputDialog("Please enter students last name");
     
    score=Double.parseDouble(JOptionPane.showInputDialog("Please enter students test score"));
     
    calcAverage( );
    {
    	double AVG;
     
     
    	AVG=(points)-(score);
     
    	if(AVG<=90)
    (LetterGrade)='A';
    	else if (AVG<=80)
    (LetterGrade)='B';
    	else if (AVG<=70)
    (LetterGrade)='C';
    	else if (AVG<=60)
    (LetterGrade)='D';
    	else
    (LetterGrade)='F';
     
    	strMessage=String.format("Your score of %.2f has earned you a letter grade of %c", AVG,LetterGrade);
     
    	JOptionPane.showMessageDialog(null, strMessage);
     
    }
    }
    }
     
    private static void calcAverage() {
    	// TODO Auto-generated method stub
     
    }
     
     
    }


    --- Update ---

    Ok I was able to get the program to work now

    My Last question if anyone could help can you help me with writing the answers to a file

    SO I am asking for students First Name, Last Name, the score and need to record the letter grade in to a file and have it append every time the program is ran

    thanks in advance

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need some help

    help me with writing the answers to a file
    Look at using the PrintWriter class. It has many methods for printing characters to a file.

    have it append every time the program
    Use the FileWriter class's constructor with the boolean flag to indicate you want the new output to be appended.
    To use FileWriter with PrintWriter, pass an instance of the FileWriter class to the PrintWriter class's constructor.
    Something like:
      ... new PrintWriter(new FileWriter(new File("the filename here")), true);
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Jan 2024
    Posts
    75
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Need some help

    Here's a possible solution for your program:

    ```java
    import javax.swing.JOptionPane;

    public class Project {
    public static void main(String[] args) {
    // Get total possible points for the test
    double totalPossiblePoints = Double.parseDouble(JOptionPane.showInputDialog("Pl ease enter the total possible points for the test"));

    // Get the number of students
    int numberOfStudents = Integer.parseInt(JOptionPane.showInputDialog("How many students do you have to enter"));

    // Loop through each student
    for (int i = 1; i <= numberOfStudents; i++) {
    // Get student's first name
    String firstName = JOptionPane.showInputDialog("Please enter student's first name");

    // Get student's last name
    String lastName = JOptionPane.showInputDialog("Please enter student's last name");

    // Get student's score
    double score = Double.parseDouble(JOptionPane.showInputDialog("Pl ease enter " + firstName + " " + lastName + "'s score"));

    // Calculate the score
    double percentage = (score / totalPossiblePoints) * 100;

    // Calculate the letter grade
    char letterGrade;
    if (percentage >= 90) {
    letterGrade = 'A';
    } else if (percentage >= 80) {
    letterGrade = 'B';
    } else if (percentage >= 70) {
    letterGrade = 'C';
    } else if (percentage >= 60) {
    letterGrade = 'D';
    } else {
    letterGrade = 'F';
    }

    // Display the result
    String message = String.format("%s %s's score is %.2f%%. Grade: %c", firstName, lastName, percentage, letterGrade);
    JOptionPane.showMessageDialog(null, message);
    }
    }
    }
    ```

    This code prompts the user for the total possible points, then the number of students, and then for each student's first name, last name, and score. After gathering the necessary information, it calculates the percentage score and assigns a letter grade based on the percentage. Finally, it displays the student's name, score percentage, and letter grade using a message dialog.

    Finally, it displays the student's name, score percentage, and letter grade. If you need further help with Java assignment, there are resources available online where you can find help with programming assignments like programminghomeworkhelp.com.

  8. #8
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: Need some help

    Why you keep replying on old post?
    Whatever you are, be a good one