本文介绍了这些switch语句有哪些替代方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是鞭打了这个非常简单的GPA计算器,想知道如何使用循环来避免switch语句的巨大块?我对Java还是很陌生,只想寻求一些建议,就可以通过其他任何方式来改进该程序.预先感谢,伙计们!

Just whipped up this extremely simple GPA calculator, wondering how I would use a loop to avoid the huge blocks of switch statements? I am very new to Java and just looking for some advice, any other ways to improve the program are greatly appreciated. Thanks in advance, guys!

package helloPackage;
import javax.swing.JOptionPane;

public class GradePointAverage {

public static void main(String[] args) {
    System.out.println("Welcome to the GPA calculator! Please enter your GPA into the popup window to find your GPA.");

    String firstClassInput = JOptionPane.showInputDialog(null,"Please enter your grade for your first class: (A+, A, A-, B+, etc.)");
    String firstClass = firstClassInput.toUpperCase();
    String secondClassInput = JOptionPane.showInputDialog(null,"Please enter your grade for your second class: (A+, A, A-, B+, etc.)");
    String secondClass = secondClassInput.toUpperCase();
    String thirdClassInput = JOptionPane.showInputDialog(null,"Please enter your grade for your third class: (A+, A, A-, B+, etc.)");
    String thirdClass = thirdClassInput.toUpperCase();
    String fourthClassInput = JOptionPane.showInputDialog(null,"Please enter your grade for your fourth class: (A+, A, A-, B+, etc.)");
    String fourthClass = fourthClassInput.toUpperCase();
    String fifthClassInput = JOptionPane.showInputDialog(null,"Please enter your grade for your fifth class: (A+, A, A-, B+, etc.)");
    String fifthClass = fifthClassInput.toUpperCase();

    double firstGrade = 0.0;
    double secondGrade = 0.0;
    double thirdGrade = 0.0;
    double fourthGrade = 0.0;
    double fifthGrade = 0.0;

    switch (firstClass){
    case "A+": firstGrade = 4.33;
        break;
    case "A": firstGrade = 4.00;
        break;
    case "A-": firstGrade = 3.67;
        break;
    case "B+": firstGrade = 3.33;
        break;
    case "B": firstGrade = 3.00;
        break;
    case "B-": firstGrade = 2.67;
        break;
    case "C+": firstGrade = 2.33;
        break;
    case "C": firstGrade = 2.00;
        break;
    case "C-": firstGrade = 1.67;
        break;
    case "D+": firstGrade = 1.33;
        break;
    case "D": firstGrade = 1.00;
        break;
    case "D-": firstGrade = .67;
        break;
    case "F": firstGrade = 0.0;
        break;
    }

    switch (secondClass){
    case "A+": secondGrade = 4.33;
        break;
    case "A": secondGrade = 4.00;
        break;
    case "A-": secondGrade = 3.67;
        break;
    case "B+": secondGrade = 3.33;
        break;
    case "B": secondGrade = 3.00;
        break;
    case "B-": secondGrade = 2.67;
        break;
    case "C+": secondGrade = 2.33;
        break;
    case "C": secondGrade = 2.00;
        break;
    case "C-": secondGrade = 1.67;
        break;
    case "D+": secondGrade = 1.33;
        break;
    case "D": secondGrade = 1.00;
        break;
    case "D-": secondGrade = .67;
        break;
    case "F": secondGrade = 0.0;
        break;
    }
    switch (thirdClass){
    case "A+": thirdGrade = 4.33;
        break;
    case "A": thirdGrade = 4.00;
        break;
    case "A-": thirdGrade = 3.67;
        break;
    case "B+": thirdGrade = 3.33;
        break;
    case "B": thirdGrade = 3.00;
        break;
    case "B-": thirdGrade = 2.67;
        break;
    case "C+": thirdGrade = 2.33;
        break;
    case "C": thirdGrade = 2.00;
        break;
    case "C-": thirdGrade = 1.67;
        break;
    case "D+": thirdGrade = 1.33;
        break;
    case "D": thirdGrade = 1.00;
        break;
    case "D-": thirdGrade = .67;
        break;
    case "F": thirdGrade = 0.0;
        break;
    }
    switch (fourthClass){
    case "A+": fourthGrade = 4.33;
        break;
    case "A": fourthGrade = 4.00;
        break;
    case "A-": fourthGrade = 3.67;
        break;
    case "B+": fourthGrade = 3.33;
        break;
    case "B": fourthGrade = 3.00;
        break;
    case "B-": fourthGrade = 2.67;
        break;
    case "C+": fourthGrade = 2.33;
        break;
    case "C": fourthGrade = 2.00;
        break;
    case "C-": fourthGrade = 1.67;
        break;
    case "D+": fourthGrade = 1.33;
        break;
    case "D": fourthGrade = 1.00;
        break;
    case "D-": fourthGrade = .67;
        break;
    case "F": fourthGrade = 0.0;
        break;
    }
    switch (fifthClass){
    case "A+": fifthGrade = 4.33;
        break;
    case "A": fifthGrade = 4.00;
        break;
    case "A-": fifthGrade = 3.67;
        break;
    case "B+": fifthGrade = 3.33;
        break;
    case "B": fifthGrade = 3.00;
        break;
    case "B-": fifthGrade = 2.67;
        break;
    case "C+": fifthGrade = 2.33;
        break;
    case "C": fifthGrade = 2.00;
        break;
    case "C-": fifthGrade = 1.67;
        break;
    case "D+": fifthGrade = 1.33;
        break;
    case "D": fifthGrade = 1.00;
        break;
    case "D-": fifthGrade = .67;
        break;
    case "F": fifthGrade = 0.0;
        break;
    }

    double total = firstGrade + secondGrade + thirdGrade + fourthGrade + fifthGrade;
    double GPA = total / 5;
    System.out.println("Your GPA is: " + GPA);
}

}

推荐答案

使用类似double getGrade(String)的方法,如下所示

Use a method like double getGrade(String) as follows

double firstGrade = getGrade(firstClass);
double secondGrade = getGrade(secondClass);
double thirdGrade = getGrade(thirdClass);
double fourthGrade = getGrade(fourthClass);
double fifthGrade = getGrade(fifthClass);

// Use your switch statement.... no break(s) because we return.
public static double getGrade(String grade) {
  switch (grade){
  case "A+": return 4.33;
  case "A": return 4.00;
  case "A-": return 3.67;
  case "B+": return 3.33;
  case "B": return 3.00;
  case "B-": return 2.67;
  case "C+": return 2.33;
  case "C": return 2.00;
  case "C-": return 1.67;
  case "D+": return 1.33;
  case "D": return 1.00;
  case "D-": return .67;
  }
  // F
  return 0.0;
}

这篇关于这些switch语句有哪些替代方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 21:44