java:不兼容的类型。必需:boolean,发现java.lang.String错误消息

时间:2016-01-31 06:44:26

标签: java

我有一个配置了学生姓名,ID和成绩等的数组。我正在编写一个从数组中提取成绩的方法,但我得到这个“布尔错误”,因为我有一个字符串。我该如何纠正?

以下是有问题的代码:

//code to print the students GPA based off the three grades received in  class
public static void print_average_grade(String myStudentID)
{
    for (Student variable : myStudent)
    {
        if (variable.getId() = (myStudentID)) //if statement is kicking back error....
        {
            double gpa = ((variable.getMyGradeA() + variable.getMyGradeB() + variable.getMyGradeC()) / 3);
            System.out.println("Student with ID# " + myStudentID + " has a GPA of " + gpa);
        }

2 个答案:

答案 0 :(得分:0)

fmt.Println("Start of session") defer fmt.Println("Room Empty") for i := 0; i < 6; i++ { s := Student{Name: "Student" + strconv.Itoa(i)} s.Present() defer s.Leave() } fmt.Println("End of session") 表示作业。为进行比较,请使用=(按引用进行比较)或==(按值进行比较)。

即,.equals()应该是variable.getId() = (myStudentID)

答案 1 :(得分:-1)

=是一个赋值运算符,这意味着它将RHS分配给LHS

foo=bar;   //the variable foo now contains the value bar

要检查是否使用==

foo==bar; //returns true or false 

但是,对于引用变量,==仅检查它们是否引用同一个对象,因此为了比较两个Strings,您应该使用

variable.getId.equals(myStudentId);  //checks if the values of the Strings is the same
相关问题