Students in this course take four exams and earn a letter grade (A+, A, A-, B+, B, B-, C+, C, C-, D+, D, D-, or F) for e
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
Students in this course take four exams and earn a letter grade (A+, A, A-, B+, B, B-, C+, C, C-, D+, D, D-, or F) for e
1 import java.util.Scanner; 2 3 /** 4 5 6 */ 7 public class Grades 8 { 9 10 11 12 13 14 15 1.C This program reads inputs that contain four grades per line and displays the average grade for each input line. Run public static void main(String[] args) { } A-B+CA ABBA CC-D+ C Scanner in new Scanner(System.in); while (processLine(in)) Grades.java { } Load default
232 24 25 26 27 28 29 30 31 32 33 34 35 36 37 public static boolean processLine (Scanner in) System.out.print("Enter four grades or Q to quit: "); // Read four grades String g1 in.next(); if (g1.equals("Q")) { return false; } String g2= in.next(); String g3 in.next(); String g4 in.next(); E // Compute and print their average A-B+ CA ABBA CC-D+ C Run double x1 = gradeToNumber(g1); double x2 aradeToNumber(a2): B
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 Converts a letter grade to a number. @param grade a letter grade (A+, A, A-, ..., D-, F) @return the equivalent number grade public static double gradeToNumber (String grade) { A- B+ CA ABBA CC-D+ C double result = 0; = String first grade.substring(0, 1); if (first.equals("A")) { result = 4; } else if (first.equals("B")) { result = 3; } else if (first.equals("C")) { result = 2; } else if (first.equals("D")) { result = 1; } if (grade.length() > 1) { String second grade.substring(1, 2); if (second.equals("+")) I