I am pretty new at coding in Java. I'm in classes now to study it and I'm having an issue with loops.
My assignment for this class is to write only ONE while loop for a program which calculates the weighted total of 3 grades. I'm using if else statements and a variable to declare what user input it should be on, however, if the input is not in a certain range, I need it to ask the same question again to the user. So far this only works with the first question, not the other two. How would I ask the same question again if the input is not valid?
Here is my code so far:
import java.util.Scanner;
public class Lab3
{
public static void main(String [] args)
{
Scanner scan = new Scanner(System.in);
double homework;
double midterm;
double finalGrade;
int i = 0;
while (i == 0) {
if (i == 0) {
System.out.print("Enter your HOMEWORK grade: ");
homework = scan.nextInt();
if (homework < 0 || homework > 100){
System.out.println("[ERR] Invalid input. A homework grade should be in [0, 100].");
}
else {
i++;
}
if (i==1) {
System.out.print("Enter your MIDTERM EXAM grade: ");
midterm = scan.nextInt();
if (midterm < 0 || midterm > 100) {
System.out.println("[ERR] Invalid input. A midterm grade should be in [0, 100].");
} else {
i++;
}
if (i==2) {
System.out.print("Enter your FINAL EXAM grade: ");
finalGrade = scan.nextInt();
if (finalGrade < 0 || finalGrade > 200) {
System.out.println("[ERR] Invalid input. A FINAL EXAM grade should be in [0, 200].");
} else {
i++;
double totalWeighted = (finalGrade / 200 * 50) + (midterm * .25) + (homework * .25);
System.out.println("[INFO] Student's Weighted Total is " + totalWeighted);
if (totalWeighted >= 50) {
System.out.println ("[INFO] Student PASSED the class.");
}
else {
System.out.println ("[INFO] Student FAILED the class.");
}
}}}}}}}
Aucun commentaire:
Enregistrer un commentaire