嘿,我最近开始使用Java,而我正在编写一个正在编写的程序。我已经看到老师如何完成他的解决方案,但是除了我在if语句中使用不同的equals之外,我没有看到我的解决方案与他的解决方案有任何区别,并且名称不同。
有人可以告诉我为什么我可能会获得0.0而不是实际的数字等级吗?
*年级*
public class Grade
{
private String grade;
public Grade(String getGrade)
{
grade = getGrade;
}
public double getNumericGrade()
{
double total = 0;
double add = 0;
String firstLetter = grade.substring(0, 1);
if (grade.length() > 1)
{
String minusOrPlus = grade.substring(1, 2);
if(minusOrPlus == "+")
add = 0.3;
if(minusOrPlus == "-")
add = -0.3;
}
if (firstLetter == "A")
{
total = 4;
if (add < 0)
total = total + add;
}
else if (firstLetter == "B")
total = 3 + add;
else if (firstLetter == "C")
total = 2 + add;
else if (firstLetter == "D")
total = 1 + add;
else if (firstLetter == "F")
total = 0;
return total;
}
}
我测试过看看我的测试仪是否错误但是如果我输入教师代码我的测试仪工作正常,所以我知道它不是那样的。但无论如何它在这里......
* GradeTester *
import java.util.Scanner;
public class GradeTester
{
public static void main (String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Hey what letter grade did you get ? : ");
String letterGrade = in.next();
Grade x = new Grade(letterGrade);
System.out.print("Your letter grade was " + letterGrade + " That means your number grade is "
+ x.getNumericGrade());
System.out.println("");
}
}
如果你能帮助我,那将是非常棒的。
谢谢, 森
答案 0 :(得分:0)
所有字符串比较都失败了:
firstLetter == "A"
应为firstLetter.equals("A")
,依此类推。