我知道有类似的线程,但我见过的没有遇到过我遇到的问题。我没有收到错误,但我的最终答案总是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);
答案 0 :(得分:1)
您需要使用grade1.isEquals(" A")来比较字符串。等级1 ==" A"比较对象标识,因为它们是不同的对象,所以它将是假的。您也可以考虑取消长期的“如果'链而不是:
answer += grade1.toLowerCase().indexOf("fdcba"); // returns the zero based index 0-4 for f-a
它更简洁一点。
答案 1 :(得分:1)
因为您使用单个字符进行比较,所以您可以将比较代码替换为:
char grade1 = grade.nextLine().charAt(0);
if(grade11 == 'A' || grade11 =='a'){
//
}
注意:并尝试使您的代码模块化。创建一个接受成绩参数并返回gpa并获取所有重复代码的方法
答案 2 :(得分:0)
所以发生的事情是因为你使用的是==
,所以你的条件都没有得到满足。在比较整数时这可行,但字符串有点不同。在基本术语中,==
检查数据的位置是否相同。您需要使用equals()
,因为它会检查数据的值是否相同。例如
if(grade1.equals("A") || grade1.equals("a")){
p1 = 4;
total+=p1;
}
This post有一些很好的答案,描述了==
和equals
之间的区别。