0

This is supposed to be a simple program that simulates a grade book. It takes input of 1st name, last name and 3 test scores. It then adds them and gets an average. The scores are limited to between 0 and 105. It then spits back the info and assigns a letter grade based on the average score, in an infinite loop.

For some reason, when it loops back it skips the 1st input and goes straight to the second. So I assumed, I had an extra newline character that needed to be consumed. I added a input.nextLine() to try and remedy it and it did fix the skipping problem. But now it doesn't run the assign a letter grade part and can not figure out why? I've tried moving the location of the input.nextLine() does nothing to fix the problem.

I am also supposed to have each step in its own method which also doesn't work for some reason as I can't figure out how to get a method to take input (I am starting to think that, I have to make a new class and then have the 2nd class create an object of the 1st class and then call on the methods, I haven't tried that yet) but I will settle on it running correctly without the methods.

// Module2_GradesTest.java
// Averaging program prompts for the student's name and three interger scores. 
// Then displays the student's name their sum & average rounded to 2 decimal place.
// contains language to limit input to scores between 0 and 105
// and outputs a letter grade based on the average

import java.util.Scanner; // program uses class Scanner

public class Module2_GradesTest
{
    //main method begins execution of Java application
    public static void main (String[] args)
    {
        //create Scanner to obtain input from the command window
        Scanner input = new Scanner(System.in);

        // declaration of variables used in program             
        int number1; // First Score
        int number2; // Second Score
        int number3; // Third Score
        int min; // minimum score
        int max; //maximum score
        double average; // average of scores 
        int sum; //sum of scores
        String student1; // student first name
        String student2; // student last name   


        //intialization of variables
        min = 0; 
        max = 105; 

        while (true)
        {
            System.out.print("Please enter student's first name: "); //prompt for student's name
                student1 = input.nextLine(); //read student's first name from user and stores it in student1 

            System.out.print("Please enter student's last name: "); //prompt for student's name
                student2 = input.nextLine(); //read student's last name from user and stores it in student2 

            System.out.println();//print an empty line

            System.out.println("Scores should be between 0 & 105"); // letting user know the limits for input

            System.out.print("Please enter student's 1st score: "); //prompt for first score
                number1 = input.nextInt(); //read first number from user 
            while (number1 < min || number1 > max)// loop prompt for 1st score until input score is between 0 & 105
            {
                System.out.print("Invalid value, please re-enter their 1st test score: ");
                    number1 = input.nextInt();
            }


            System.out.print("Please enter student's 2nd score: "); //prompt for second score
                number2 = input.nextInt(); //read second number from user 
            while (number2 < min || number2 > max)// loop prompt for 2nd score until input score is between 0 & 105
            {
                System.out.print("Invalid value, please re-enter their 2nd test score: ");
                    number2 = input.nextInt();
            }


            System.out.print("Please enter student's 3rd score: "); //prompt for third score
                number3 = input.nextInt(); //read third number from user 
            while (number3 < min || number3 > max) // loop prompt for 3rd score until input score is between 0 & 105
            {
                System.out.print("Invalid value, please re-enter their 3rd test score: ");
                    number3 = input.nextInt();
            }

            sum = (number1 + number2 + number3); //totals scores and stores it in sum

            average = (sum/3.00); //averages scores and store in average

            System.out.println();// print an empty line

            System.out.printf("%s %s's scores are: %d, %d, and %d.%n" ,student1 ,student2 ,number1 ,number2 ,number3);
            //displays student name and their scores

            System.out.printf("Their sum and average score are %d & %.2f." ,sum ,average);
            //displays student name and their sum and their average


            //based upon Average, this states their Alphabetical grade

            if (average >= 90) 
                System.out.printf("%n%s %s has an A.%n",student1 ,student2); 

            else if ((average >= 80) && (average < 89.99)) 
                System.out.printf("%n%s %s has a B.%n",student1 ,student2);

            else if ((average >= 70) && (average < 70.99)) 
                System.out.printf("%n%s %s has a C.%n",student1 ,student2);

            else if ((average < 70)) 
                System.out.printf("%n%s %s has a F.%n",student1 ,student2);

            System.out.println();// print an empty line
            input.nextLine();// consume extra newLine

        }//end loop delclaration
    }// end of method main
}// end of class Module2_GradesTest

No comp or syntax errors just doesn't do what it is supposed to.

notescrew
  • 3,072
  • 4
  • 21
  • 36
  • Did the "assign a letter grade" part _ever_ work? Your range for a B is 80--89.99, but your range for a C is 70--70.99. What happens if the average is, say, 74.0? – Kevin Anderson Oct 04 '19 at 04:02
  • that might be the problem actually. Let me see if that is the problem. At one point it was working intermittently. I didn't catch that little mistake. – Sam Carter Oct 04 '19 at 04:04
  • That was indeed the problem... I feel kind of dumb now. I thought it was the nextline issue that I had already read about in the duplicate question post that Elliot Frisch referred me too. Thanks Kevin Anderson. Apparently I have been staring at this little bit of code for too long that I just read right over that my range was incorrect. – Sam Carter Oct 04 '19 at 04:07
  • Also, trying to "bracket" `double` values on both sides is risky: consider that there are `double` values of 79.99 or greater which are still strictly less than 80.00; neither the test for "B" nor the test for "C" would catch, say 79.9993432. Just test for the low end of each range, the `else if` cascade will take care of the high end. – Kevin Anderson Oct 04 '19 at 04:11

0 Answers0