In: Computer Science
I have entered this line of code for exercise 6-8 saying to Write an application that allows a user to enter any number of student quiz scores, as integers, until the user enters 99. If the score entered is less than 0 or more than 10, display Score must be between 10 and 0 and do not use the score. After all the scores have been entered, display the number of valid scores entered, the highest score, the lowest score, and the arithmetic average. I only have 96%. done with one result missing saying that the score must be between 10 and 0 even though i see that its there, what do i need to change if anything?
import java.util.*;
public class QuizScoreStatistics {
public static void main(String[] args) {
int quizScore = 0, validScores = 0;
int highestScore = 0;
int lowestScore = 10;
double sum =0, average=0.0;
Scanner scan = new Scanner(System.in);
do {
//prompt & read score from user
System.out.print("Enter the quiz score : ");
quizScore = scan.nextInt();
if(quizScore == 99)
break;
if(quizScore<0 || quizScore>10) {
System.out.println("Score must must be between 10 and 0: ");
}
else {
//Calculate sum of quiz score
sum += quizScore;
//Increment valid score counter
validScores++;
//Determine highest score
if(quizScore > highestScore)
highestScore = quizScore;
//Determine lowest score
if(quizScore < lowestScore)
lowestScore = quizScore;
}
}while(true);
//calculating the average
average = sum / validScores;
//Printing the results
System.out.println("Number of valid scores: " + validScores);
System.out.println("Highest Score: " + highestScore);
System.out.println("Lowest Score: " + lowestScore);
System.out.println("Arithmetic average: " + String.format("%.2f", average));
}
}
The given program works perfectly and found no error.But in quiz score validation,the out put message is "Score must must be between 10 and 0: ". The word "must" is repeated and there is a colon also.Please eliminate the repeated word and the colon(:).The correct statement would be System.out.println("Score must be between 10 and 0"); .Please try this change,the problem will be solved.If find any difficulty,feel free to ask in comment section.Please do upvote the answer.Thank you.
Source code with small correction
import java.util.Scanner; public class QuizScoreStatistics { public static void main(String[] args) { int quizScore = 0, validScores = 0; int highestScore = 0; int lowestScore = 10; double sum =0, average=0.0; Scanner scan = new Scanner(System.in); do { //prompt & read score from user System.out.print("Enter the quiz score : "); quizScore = scan.nextInt(); if(quizScore == 99) break; if(quizScore<0 || quizScore>10) { System.out.println("Score must be between 10 and 0"); } else { //Calculate sum of quiz score sum += quizScore; //Increment valid score counter validScores++; //Determine highest score if(quizScore > highestScore) highestScore = quizScore; //Determine lowest score if(quizScore < lowestScore) lowestScore = quizScore; } }while(true); //calculating the average average = sum / validScores; //Printing the results System.out.println("Number of valid scores: " + validScores); System.out.println("Highest Score: " + highestScore); System.out.println("Lowest Score: " + lowestScore); System.out.println("Arithmetic average: " + String.format("%.2f", average)); } }
Screen shot of the output