I am almost done with this but there are 2 things that are stumping me in my code. When I query a user for the test score, if the score is not within the 0-100 range, I want to not accept it and then tell them why and ask for another input. I also want to print the letter grade for their average next to their average score. For some reason my If logic statement isn't working when I try to check to make sure the score entered is within 0-100. Also I can't figure out how to get the letter grade to print, but I am not getting any error output so I think that I am on the right track. I think that I could mainly use pointers on my while looping to check for the number being in the range of 0-100. I would greatly appreciate it. Here is my code:
import java.text.DecimalFormat;
import java.util.Scanner;
public class GradeReport
{
String name;
int score1, score2, score3;
double average;
String grade;
public GradeReport() //creates the first constructor
{
Scanner sc = new Scanner (System.in);
System.out.println ("Enter student's name: ");
name = sc.nextLine();
System.out.println ("Enter first grade: "); //try while loops to get grade in between 0-100
score1 = sc.nextInt();
while
(score1 <0 || score1 > 100);
System.out.println("please enter a grade 0-100"); //checks that score is inclusive 1-100
System.out.println ("Enter second grade: ");
score2 = sc.nextInt();
while
score2 <0 || score2 > 100;
System.out.println("please enter a grade 0-100");//checks that score is inclusive 1-100
System.out.println ("Enter third grade: ");
score3 = sc.nextInt();
while
score3 <0 || score3 >100;
System.out.println("please enter a grade 0-100");//checks that score is inclusive 1-100
}
public GradeReport (String v1, int v2, int v3, int v4)
{
name = v1; //these are to initialize the variables so that I don't get null for the second set of results.
score1 = v2;
score2 = v3;
score3 = v4;
}
public void calculateAvg()
{
average = (double)((score1 + score2 + score3) / 3.0);
}
public String calculateGrade()
{
if (average >= 90)
grade = "A";
else if (average >= 80)
grade = "B";
else if (average >= 70)
grade = "C";
else if (average >= 60)
grade = "D";
else
grade = "F";
return grade;
}
public String toString()
{
DecimalFormat fmt = new DecimalFormat ("0.00"); //to format average to 2 decimal places
String gradeReport = name + "\n " + Double.toString(score1) + "\t" + Double.toString(score2)+ "\t" + Double.toString(score3) + "\n" + fmt.format(average) + grade;
return gradeReport;
}
public static void main (String[] args)
{
GradeReport gr1 = new GradeReport();
GradeReport gr2 = new GradeReport("Col Een", 76, 76, 75);
gr1.calculateAvg();
gr1.calculateGrade();
gr2.calculateAvg();
gr2.calculateGrade();
System.out.println(gr1);
System.out.println(gr2);
}
}