This code asks for a score between 0-100. If the score is in between, it is added to the total and later used to calculate the average. The problem I am having is placing the if statement if the score is greater than 100, printing an illegal statement and asking the user to retype the number. When I put this if, in the while loop, I am getting an infinite number of illegal exceptions printed out. how do I fix this problem?
public class SentinalValuedControlledLoop {
public static void main(String[] args) {
int studentCount = 1;
double total = 0.0;
double average;
int score;
Scanner stdin = new Scanner(System.in);
//title at top of output
System.out.println("Sai Bharathula's Score Report");
//read score for student
System.out.printf("Enter a score (0 too 100, -1 to quit #%d:)", studentCount);
score = stdin.nextInt();
while(score !=-1) {
//THIS IS THE IF STATEMENT I AM TALKING ABOUT THAT IS CAUSING ME TROUBLE
if(score >100){
System.out.println("Illegal Score Try Again");
}
if(score >0 && score <= 100) {
System.out.printf ("Enter a score (0 too 100, -1 to quit #%d:)", studentCount);
score = stdin.nextInt();
studentCount++;
}
}
average = total / studentCount;
System.out.printf ("\nThe average score for %d students is %8.2f\n",
studentCount, average);
}
}
Aucun commentaire:
Enregistrer un commentaire