(JAVA) Write a program that accepts user input for five values of type double, a
ID: 3728082 • Letter: #
Question
(JAVA) Write a program that accepts user input for five values of type double, and prints their average. You do not need to declare five different variables, think of how you can get away with a single variable. Do not use any while or for loops. The second part of your program will be to figure out whether the average of these 5 numbers results in grade 100>A>=90, 90>=B>=80, 80>C>=70, 70>D>=60 or F<60, you are required to use a switch-case statement with cases for each grade.
Explanation / Answer
/**The java program that prompts user to enter
* five scores and then calculate average of scores
* and then find the grade of averages.*/
//GradeAverage.java
import java.util.Scanner;
public class GradeAverage {
public static void main(String[] args) {
//Create an instance of Scanner class
Scanner scanner=new Scanner(System.in);
double total_score=0;
double avg=0;
String grade;
//prompt for score1
System.out.println("Enter score1 : ");
total_score=Double.parseDouble(scanner.nextLine());
//prompt for score2
System.out.println("Enter score2 : ");
total_score+=Double.parseDouble(scanner.nextLine());
//prompt for score3
System.out.println("Enter score3 : ");
total_score+=Double.parseDouble(scanner.nextLine());
//prompt for score4
System.out.println("Enter score4 : ");
total_score+=Double.parseDouble(scanner.nextLine());
//prompt for score5
System.out.println("Enter score5 : ");
total_score+=Double.parseDouble(scanner.nextLine());
//calculate average of scores
avg=total_score/5.0;
//Calculate quotient of average
int q=(int) (avg/10);
switch (q)
{
case 10:
case 9:
//90-100
grade="A";
break;
case 8:
//80-89
grade="B";
break;
case 7:
//70-79
grade="C";
break;
case 6:
//60-69
grade="D";
break;
default:
// Anything 59 or below is an F
grade= "F";
}
//print Grade value.
System.out.println("Grade : "+grade);
}
}
-------------------------------------------------------------------
Sample Output :
Enter score1 :
90
Enter score2 :
99
Enter score3 :
90
Enter score4 :
90
Enter score5 :
90
Grade : A
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.