gpt4 book ai didi

java - 这些 switch 语句有哪些替代方案?

转载 作者:行者123 更新时间:2023-11-30 06:21:02 25 4
gpt4 key购买 nike

刚刚创建了这个极其简单的 GPA 计算器,想知道如何使用循环来避免大量的 switch 语句 block ?我是 Java 的新手,只是在寻找一些建议,非常感谢任何其他改进程序的方法。提前致谢,伙计们!

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)的方法如下

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;
}

关于java - 这些 switch 语句有哪些替代方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20941509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com