Question: how to add function to promp user for numeric input only so it doesnt break the code ? import java.util.Scanner; class TestGrade { //main method
how to add function to promp user for numeric input only so it doesnt break the code ?
import java.util.Scanner; class TestGrade { //main method public static void main (String arg[]) { //declare the required variables double score1, score2, score3, score4, score5; char grade1, grade2, grade3, grade4, grade5; double avg; //read input from the user and store value //into the variable score1 System.out.println("Please Enter a Score:"); Scanner sc= new Scanner(System.in); score1 = sc.nextDouble(); //determine to get grade of score1 grade1= determineGrade(score1); //read input from the user and store value System.out.println("Please enter a Score:"); Scanner st = new Scanner(System.in); score2 = st.nextDouble(); //read input from users and store the value grade2=determineGrade(score2); //read input from the user and store value System.out.println("Please Enter a Score:"); Scanner se = new Scanner(System.in); score3 = se.nextDouble(); //call determineGrade to get the grade grade3=determineGrade(score3); System.out.println("Please Enter a Score:"); Scanner sd= new Scanner(System.in); score4 = sd.nextDouble(); grade4= determineGrade(score4); System.out.println("Please enter a Score:"); Scanner sg= new Scanner(System.in); score5 = sg.nextDouble(); //call all scanner objects st.close(); st.close(); st.close(); st.close(); st.close(); grade5= determineGrade(score5); System.out.println(" "); //call the calcAverage() method by passing avg = calcAverage(score1,score2,score3,score4,score5); //print the results System.out.println("Score Letter Grade"); System.out.println("_________"); System.out.println("Score 1: " + grade1 ); System.out.println("Score 2: " + grade2 ); System.out.println("Score 3: " + grade3); System.out.println("Score 4: " + grade4); System.out.println("Score 5: " + grade5); System.out.println("The average test "+ "score is:" + avg); } //determineGrade() //returns a char which holds the letter grade public static char determineGrade(double score) { //declare the char variable char grade; //condition to check for the score if (score >= 90 && score <= 100) grade='A'; else if (score >= 80 && score <= 89) grade = 'B'; else if (score >= 70 && score <= 79) grade = 'C'; else if (score >= 60 && score <= 69) grade = 'D'; else grade = 'F'; //return grade value return grade; } //calcAverage public static double calcAverage(double score1, double score2, double score3, double score4, double score5) { return (score1+ score2+ score3+ score4+ score5)/5.0 ; } } Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
