刚刚开始使用这个非常简单的GPA计算器,想知道如何使用循环来避免大量的switch语句?我是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);
}
}
答案 0 :(得分:2)
使用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;
}
答案 1 :(得分:1)
您可以轻松创建一个Enum来表示这些成绩,并将成绩的值作为字段添加到枚举中。
答案 2 :(得分:1)
不使用firstGrade
,secondgrade
等,而是使用数组或列表:
double[] grades = new double[5]; // firstGrade will be grades[0], and so on.
对于字母成绩,我建议使用枚举,使用valueOf()
方法。
答案 3 :(得分:1)
此方法删除所有开关,并仅使用两个if语句:
public static double getGrade(String grade) {
double retVal = 0.0;
char letter = grade.getCharAt(0); //Get the letter grade
//Compute the base grade--some of these casts may not be needed
//(I'm not figuring it out right now)
retVal = Math.max((int)'A' - (int)letter + 4, 0.0);
if (grade.length() == 1) return retVal;
//Alter the grade based on the postfix
char pm = grade.getCharAt(1); //Get the plus/minus
if (pm == '+') retVal += 0.33;
else retVal -= 0.33;
return retVal;
}
然后,使用“Chthonic Project”答案中提到的数组方法。
答案 4 :(得分:0)
您只需要一个大的switch语句。
而不是拥有firstClass / firstGrade / secondClass / secondGrade,只需要两个数组,class []和grade []。然后你可以做类似的事情:
for(int i = 0; i < 5; i++){
switch(class[i]){
case "A+" : grade[i] = 4.33;
}
}
答案 5 :(得分:0)
如果将一些类似的任务封装到单独的方法中,它应该使您的代码更清晰一些。试试这个。希望这有帮助
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();
List<String> listOfGrades = new ArrayList<String>();
listOfGrades.add(firstClass);
listOfGrades.add(secondClass);
listOfGrades.add(thirdClass);
listOfGrades.add(fourthClass);
listOfGrades.add(fifthClass);
double GPA = getGPA( listOfGrades );
System.out.println("Your GPA is: " + GPA);
}
/**
* Used to calculate GPA from list of supplied grades
*
* @param listOfGrades as list of grades to compute GPA
* @return GPA as double
*/
public static double getGPA(List<String> listOfGrades){
double total = 0.0;
double GPA = 0.0;
for(String aGrade: listOfGrades){
total += getGrade( aGrade );
}
GPA = total / listOfGrades.size();
System.out.println("Total "+total+" amount of grades: "+listOfGrades.size());
return GPA;
}
/**
* Get value of grade as a double type
*
* @param stringGrade Grade
* @return value of grade as
*/
public static double getGrade(String stringGrade ){
double grade = 0.0;
switch (stringGrade) {
case "A+":
grade = 4.33;
break;
case "A":
grade = 4.00;
break;
case "A-":
grade = 3.67;
break;
case "B+":
grade = 3.33;
break;
case "B":
grade = 3.00;
break;
case "B-":
grade = 2.67;
break;
case "C+":
grade = 2.33;
break;
case "C":
grade = 2.00;
break;
case "C-":
grade = 1.67;
break;
case "D+":
grade = 1.33;
break;
case "D":
grade = 1.00;
break;
case "D-":
grade = .67;
break;
case "F":
grade = 0.0;
break;
}
System.out.println("stringGrade: "+stringGrade+"="+grade);
return grade;
}
答案 6 :(得分:0)
我建议使用枚举或地图,主要是因为有最人性化的替代品,它也为您提供了一个很好的机会来解耦数据UI /逻辑层。
示例枚举:
public enum Grades {
APLUS("A+", 4.33),
A("A",4.00);
private String label;
private double value;
private Grades(String label,double value){
this.label = label;
this.value = value;
}
public double getValue() {
return value;
}
public static Grades getByLabel(String label){
for(Grades grades : values()){
if(grades.label.equals(label)){
return grades;
}
}
throw new IllegalArgumentException("Incorrect Label");
}
}
稍后在您的代码Grades.getByLabel(firstGrade).getValue();
中使用
如果您计划添加连接到每个等级的任何额外数据,则枚举非常好。一些其他类型的测量或类似,那么就不需要几个数组,列表或类似的。
示例地图(在单独的类中):
public class GradeValues {
private static Map<String,Double> gradeValues = new HashMap<String, Double>();
static{
gradeValues.put("A+", 4.33);
gradeValues.put("A", 4.00);
}
public static double getGradeValue(String grade){
return gradeValues.get(grade);
}
}
稍后在您的代码中使用:
GradeValues.getGradeValue(firstGrade);