Java GPA 计算结果问题

Java GPA Calc result Issues

GPA 计算器

我知道那里有类似的线程,但我看到的 none 遇到了我遇到的问题。我没有收到任何错误,但我的最终答案始终是 0 而不是实际 GPA 值。任何帮助将不胜感激! 注意:我是 Java 的新手,这是我阅读 Java 数小时后的第一个正式项目。

public static void main (String[]args){


    double total = 0,total2=0, total3=0, total4=0, total5=0, total6=0;
    double classes=0;
    double answer=0;
    String grade1,grade2,grade3,grade4,grade5,grade6;
    int p1, p2, p3, p4, p5, p6;



System.out.println("Welcome to the Gpa Calc");
System.out.println("Please enter a grade for every class you have.");
System.out.println("Shall we begin?");
System.out.println("How many classes do you have?");
    Scanner c = new Scanner(System.in);
    classes= c.nextInt();

    Scanner grade = new Scanner(System.in);
    System.out.println("What is your Grade in your first period class?");
    grade1 = grade.nextLine();
    if(grade1 == "A" || grade1 =="a"){
         p1 = 4;
         total+=p1;
    }
    else if(grade1 == "B" || grade1 =="b"){
        p1= 3;
        total+= p1;
    }
    else if(grade1 == "C" || grade1 =="c"){
        p1= 2;
        total+= p1;
    }
    else if(grade1 == "D" || grade1 =="d"){
        p1= 1;
        total += p1;
    }
    else if(grade1 == "F" || grade1 =="f"){
        p1= 0;
        total += p1;
    }   

    System.out.println("What is your Grade in your second period class?");
    grade2 = grade.nextLine();

    if(grade2 == "A" || grade2 =="a"){
         p2 = 4;
         total2 += p2;
    }
    else if(grade2 == "B" || grade2 =="b"){
         p2 = 3;
         total2 += p2;
    }
    else if(grade2 == "C" || grade2 =="c"){
         p2 = 2;
         total2 += p2;
    }
    else if(grade2 == "D" || grade2 =="d"){
         p2 = 1;
         total2 += p2;
    }
    else if(grade2 == "F" || grade2 =="f"){
         p2 = 0;
         total2 += p2;
    }   

    System.out.println("What is your Grade in your third period class?");
    grade3 = grade.nextLine();

    if(grade3 == "A" || grade3 =="a"){
         p3 = 4;
         total3 += p3;
    }
    else if(grade3 == "B" || grade3 =="b"){
         p3 = 3;
         total3 += p3;
    }
    else if(grade3 == "C" || grade3 =="c"){
         p3 = 2;
         total3 += p3;
    }
    else if(grade3 == "D" || grade3 =="d"){
         p3 = 1;
         total3 += p3;
    }
    else if(grade3 == "F" || grade3 =="f"){
         p3 = 0;
         total3 += p3;
    }   

    System.out.println("What is your Grade in your fourth period class?");
    grade4 = grade.nextLine();

    if(grade4 == "A" || grade4 =="a"){
         p4 = 4;
         total4 += p4;
    }
    else if(grade4 == "B" || grade4 =="b"){
         p4 = 3;
         total4 += p4;
    }
    else if(grade4 == "C" || grade4 =="c"){
         p4 = 2;
         total4 += p4;
    }
    else if(grade4 == "D" || grade4 =="d"){
         p4 = 1;
         total4 += p4;
    }
    else if(grade4 == "F" || grade4 =="f"){
         p4 = 0;
         total4 += p4;
    }   

    System.out.println("What is your Grade in your fifth period class?");
    grade5 = grade.nextLine();

    if(grade5 == "A" || grade5 =="a"){
         p5 = 4;
         total5 += p5;
    }
    else if(grade5 == "B" || grade5 =="b"){
         p5 = 3;
         total5 += p5;
    }
    else if(grade5 == "C" || grade5 =="c"){
         p5 = 2;
         total5 += p5;
    }
    else if(grade5 == "D" || grade5 =="d"){
         p5 = 1;
         total5 += p5;
    }
    else if(grade5 == "F" || grade5 =="f"){
         p5 = 0;
         total5 += p5;
    }   
    System.out.println("What is your Grade in your sixth period class?");
    grade6 = grade.nextLine();

    if(grade6 == "A" || grade6 =="a"){
         p6 = 4;
         total6 += p6;
    }
    else if(grade6 == "B" || grade6 =="b"){
         p6 = 3;
         total6 += p6;
    }
    else if(grade6 == "C" || grade6 =="c"){
         p6 = 2;
         total6 += p6;
    }
    else if(grade6 == "D" || grade6 =="d"){
         p6 = 1;
         total6 += p6;
    }
    else if(grade6 == "F" || grade6 =="f"){
         p6 = 0;
         total6 += p6;
    }   


    answer = total + total2 + total3 + total4 + total5 + total6 ;
    double GPA = answer/classes;
    System.out.println("Your current unweighed GPA is " +GPA);

您需要使用grade1.isEquals("A")来比较字符串。 grade1 == "A" 比较对象标识,这将是错误的,因为它们是不同的对象。您也可以考虑取消长 'if' 链,而是使用:

answer += grade1.toLowerCase().indexOf("fdcba"); // returns the zero based index 0-4 for f-a

简洁一点。

所以发生的事情是你的 none 个条件被满足,因为你正在使用 ==。这在比较整数时效果很好,但字符串有点不同。在基本术语中 == 检查数据的位置是否相同。您需要使用 equals() 来代替,因为它会检查数据的值是否相同。例如

if(grade1.equals("A") || grade1.equals("a")){
     p1 = 4;
     total+=p1;
}

This post 有一些很好的答案描述了 ==equals 之间的区别。

因为您使用的是单个字符进行比较,所以您可以将比较代码替换为:

  char grade1 = grade.nextLine().charAt(0);
       if(grade11 == 'A' || grade11 =='a'){
            // 
        }

注意:同时尝试使您的代码模块化。创建一个接受成绩参数和 return gpa 的方法并获取所有重复代码