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.