Step 4: Add code that discards any extra entries at the propmt that asks if you
ID: 3665952 • Letter: S
Question
Step 4: Add code that discards any extra entries at the propmt that asks if you want to enter another score.
Notes from professor:
For Step 4, add a loop that will validate the response for the prompt question:
"Enter another test score? (y/n): "
This loop must only accept the single letters of ‘y’ or ‘n’ (upper case is okay). I suggest that you put this loop inside the loop that already determines if the program should collect more input, after the code that calculates the average.
Here is what I have so far:
import java.util.Scanner;
import java.text.NumberFormat;
public class TestScoreApp
{
public static int getIntWithinRange(Scanner sc, String prompt,
int min, int max)
{
int i = 0;
boolean isValid = false;
while (isValid == false)
{
i = getInt(sc, prompt);
if (i <= min)
System.out.println(
"Error! Number must be greater than " + min + ".");
else if (i >= max)
System.out.println(
"Error! Number must be less than " + max + ".");
else
isValid = true;
}
return i;
}
public static int getInt(Scanner sc, String prompt)
{
int i = 0;
boolean isValid = false;
while (isValid == false)
{
System.out.print(prompt);
if (sc.hasNextInt())
{
i = sc.nextInt();
isValid = true;
}
else
{
System.out.println("Error! Invalid integer value. Try again.");
}
sc.nextLine(); // discard any other data entered on the line
}
return i;
}
public static void main(String[] args)
{
int scoreTotal = 0;
int scoreCount = 0;
int testScore = 0;
double averageScore = 0;
Scanner sc = new Scanner(System.in);
String choice = "y";
while (!choice.equalsIgnoreCase("n"))
{
// get the input from the user
System.out.print("Enter score: ");
testScore = sc.nextInt();
testScore = getIntWithinRange(sc,"Enter score: ",1,100);
scoreCount += 1;
scoreTotal += testScore;
averageScore = (double) scoreTotal / (double) scoreCount;
// see if the user wants to enter more test scores
System.out.print("Enter another test score? (y/n): ");
choice = sc.next();
}
NumberFormat number = NumberFormat.getNumberInstance();
number.setMaximumFractionDigits(1);
String message = " " +
"Score count: " + scoreCount + " "
+ "Score total: " + scoreTotal + " "
+ "Average score: " + number.format(averageScore) + " ";
System.out.println(message);
}
}
Explanation / Answer
import java.util.Scanner;
import java.text.NumberFormat;
public class TestScoreApp
{
public static int getIntWithinRange(Scanner sc, String prompt,
int min, int max)
{
int i = 0;
boolean isValid = false;
while (isValid == false)
{
i = getInt(sc, prompt);
if (i <= min)
System.out.println(
"Error! Number must be greater than " + min + ".");
else if (i >= max)
System.out.println(
"Error! Number must be less than " + max + ".");
else
isValid = true;
}
return i;
}
public static int getInt(Scanner sc, String prompt)
{
int i = 0;
boolean isValid = false;
while (isValid == false)
{
System.out.print(prompt);
if (sc.hasNextInt())
{
i = sc.nextInt();
isValid = true;
}
else
{
System.out.println("Error! Invalid integer value. Try again.");
}
sc.nextLine(); // discard any other data entered on the line
}
return i;
}
public static void main(String[] args)
{
int scoreTotal = 0;
int scoreCount = 0;
int testScore = 0;
double averageScore = 0;
Scanner sc = new Scanner(System.in);
String choice = "y";
while (!choice.equalsIgnoreCase("n"))
{
// get the input from the user
System.out.print("Enter score: ");
testScore = sc.nextInt();
testScore = getIntWithinRange(sc,"Enter score: ",1,100);
scoreCount += 1;
scoreTotal += testScore;
averageScore = (double) scoreTotal / (double) scoreCount;
// see if the user wants to enter more test scores
System.out.print("Enter another test score? (y/n): ");
choice = sc.next();
// Check if the anwer choice is neither equal to y nor n.
while(!choice.equalsIgnoreCase("n") && !choice.equalsIgnoreCase("y")){
System.out.print("Invalid input, please enter (y/n)? : ");
choice = sc.next();
}
}
NumberFormat number = NumberFormat.getNumberInstance();
number.setMaximumFractionDigits(1);
String message = " " +
"Score count: " + scoreCount + " "
+ "Score total: " + scoreTotal + " "
+ "Average score: " + number.format(averageScore) + " ";
System.out.println(message);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.