gpt4 book ai didi

Java GPA 计算结果问题

转载 作者:行者123 更新时间:2023-11-30 03:32:41 24 4
gpt4 key购买 nike

GPA计算器

我知道那里有类似的线程,但我见过的没有一个遇到我遇到的问题。我没有收到任何错误,但我的最终答案始终是 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"比较对象标识,由于它们是不同的对象,因此结果为 false。您也可以考虑去掉长的“if”链,而是使用:

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

更简洁一点。

关于Java GPA 计算结果问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28655322/

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