【发布时间】:2018-01-14 17:13:38
【问题描述】:
package studentclient;
import java.util.Scanner;
public class StudentClient {
public static void main(String[] args) {
/* Declare two object references of type Student s1 and s2 and instantiate two Student objects passing three arguments to the constructor for the class. Use different values for each class object */
Student s1 = new Student("Dick Grayson", "666-66-6666", 3.80 );
Student s2 = new Student("Jason Todd", "666-77-7777", 3.50 );
Scanner scan = new Scanner(System.in);
/* Output the name, social security number and GPA of the student from object reference s1 using the appropriate accessor methods(Get methods) to obtain the data */
System.out.println("Student Information");
System.out.println("Student Name: " + s1.getName());
System.out.println("Student Social Security Number: " + s1.getSsn());
System.out.println("Student GPA: " + s1.getGpa());
/* Output the name, social security number and GPA of the student from object reference s2 using the toString method to return the data */
System.out.println("Student Information");
System.out.print(s2);
/* Using the equals method and a selection control structure (if statement), compare objects s1 and s2 and output an appropriate message indicating if the objects are equal */
if(s1.equals(s2)){
System.out.println("Student's identities are equal!");
}else {
System.out.println("Student's identities are not equal!");
}
/* Using the appropriate mutator methods(Set Methods) on student object s2, change the name, social security number and GPA to the same values as in object s1. Use the set methods. */
System.out.println("Enter new Student Information for Student 2: ");
s2.setName("Dick Grayson");
System.out.println("Student Name: " + s2.getName());
s2.setSsn("666-66-6666");
System.out.println("Student Social Security Number: " + s2.getSsn());
s2.setGpa( 3.80 );
System.out.println("Student GPA: " + s2.getGpa());
/* Again, using the equals method and a selection control structure (if statement), compare objects s1 and s2 and output an appropriate message indicating if the objects are equal */
if(s1.equals(s2)){
System.out.println("Student's identities are equal!");
}else{
System.out.println("Student's identities are not equal!");
}
}
}
问题在于,对于第二组信息,s2 显然等于 s1,但我的结果一直说它们不相等。我该怎么办? 当我运行它时,我会得到这些结果
学生信息 学生姓名:迪克·格雷森 学生社会安全号码:666-66-6666 学生平均绩点:3.8 学生信息 姓名:杰森·托德 社会安全号码:666-77-7777 平均绩点:3.5 学生身份不平等! 输入学生 2 的新学生信息: 学生姓名:迪克·格雷森 学生社会安全号码:666-66-6666 学生平均绩点:3.8 学生身份不平等! 构建成功(总时间:0 秒)
【问题讨论】:
-
注意正确格式化您的问题。 how to ask
-
问题是学生的equals方法
-
如果没有
Student.equals的定义,这很难说什么是错的。如上所述,提供minimal reproducible example,我的意思是,一个非常小的示例;)