Question: How do I make a case statement of grades A, B, C, D, F thtas lets user input grade and gets average grades and highest
How do I make a case statement of grades A, B, C, D, F thtas lets user input grade and gets average grades and highest grade (in java)
Here is the code I have now
package com.company; import java.util.Scanner;
public class Main {
public static void main(String[] args) { Scanner keyboard = new Scanner(System.in);
// If the number of grades to be entered is known double grade; double total = 0;
for (int i = 0; i < 5; i++) { System.out.print("Please enter grade: "); grade = keyboard.nextDouble(); total += grade; // same as total = total + grade }
System.out.println("The average score is: " + (total/5));
System.out.println(" ");
// If the number of grades to entered is unknown double totalNew = 0; int count = 0; int userInput;
System.out.print("Please enter grade (Enter -1 to exit): "); userInput = keyboard.nextInt();
while (userInput != -1) { totalNew += userInput; count++; System.out.print("Please enter grade (Enter -1 to exit): "); userInput = keyboard.nextInt(); }
if (count > 0) System.out.println("The average grade is " + (totalNew/count));
System.out.println(" ");
// Using methods
double gradeTotal = 0; int gradeCount = 0; int userGrade = 0;
// Prime the while loop System.out.print("Enter your choice (1 for a new grade, 2 for displaying the average grade and 3 to exit): "); int choice = keyboard.nextInt();
while (choice != 3) { if (choice == 1) { userGrade = getUserGrade(); gradeTotal += userGrade; gradeCount++; } else { displayAverageGrade(gradeTotal, gradeCount); }
// Prime the while loop System.out.print("Enter your choice (1 for a new grade, 2 for displaying the average grade and 3 to exit): "); choice = keyboard.nextInt(); }
}
public static int getUserGrade() { Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter grade: "); int userInput = keyboard.nextInt();
return userInput; }
public static void displayAverageGrade(double gradeTotal, int gradeCount) { if (gradeCount > 0) System.out.println("The average grade is " + (gradeTotal / gradeCount)); else System.out.println("No grade entered"); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
