测试者返回false时应该为同名程序返回true?

时间:2015-10-03 20:28:53

标签: oop methods

这是我的方法

       public boolean equals(Name otherName)
       {  
          return (myFirstname.equalsIgnoreCase(otherName)) && (myMiddlename.equalsIgnoreCase(otherName)) && (myLastname.equalsIgnoreCase(otherName));
       }

       public String initials()
       {
          String initials = "";
          initials = myFirstname.substring(0,0) + myMiddlename.substring(0,0) + myLastname.substring(0,0);
          initials.toUpperCase();
          return initials;
       }

       public int length()
       {
          int length;
          length = myFirstname.length() + myMiddlename.length() + myLastname.length();
          return length;
       }


    }

这是我的测试员

import java.util.Scanner;
public class NameTester
{
   public static void main(String[] args)
   {
      Scanner scanner = new Scanner(System.in);
      System.out.println("Enter your first name.");
      String fname = scanner.nextLine ();
      System.out.println("Enter your middle name.");
      String mname = scanner.nextLine ();
      System.out.println("Enter your last name.");
      String lname = scanner.nextLine ();

      Name namea = new Name(fname,mname,lname);

      System.out.println("Enter your first name.");
      fname = scanner.nextLine ();
      System.out.println("Enter your middle name.");
      mname = scanner.nextLine ();
      System.out.println("Enter your last name.");
      lname = scanner.nextLine ();

      Name nameb = new Name(fname,mname,lname);

      System.out.println("Enter your first name.");
      fname = scanner.nextLine ();
      System.out.println("Enter your middle name.");
      mname = scanner.nextLine ();
      System.out.println("Enter your last name.");
      lname = scanner.nextLine ();

      Name namec = new Name(fname,mname,lname);

          System.out.println(namea.firstMiddleLast());
          System.out.println(namea.lastFirstMiddle());
          System.out.println(namea.initials());
          System.out.println(namea.length());

          System.out.println(nameb.firstMiddleLast());
          System.out.println(nameb.lastFirstMiddle());
          System.out.println(nameb.initials());
          System.out.println(nameb.length());

          System.out.println(namec.firstMiddleLast());
          System.out.println(namec.lastFirstMiddle());
          System.out.println(namec.initials());
          System.out.println(namec.length());

          System.out.println(namea.equals(nameb)); 
          System.out.println(namec.equals(nameb));
          System.out.println(namea.equals(namec)); 
   }

由于某种原因它仍然为(namea.equals(nameb))输出false,即使它们在我输入时是相同的,我认为它与我的equals方法有关。其他一切正常,除了当我在标题中用字符串替换Name时,它编译并运行错误的答案但是当我把它作为名称时它甚至不编译

2 个答案:

答案 0 :(得分:0)

我认为您的equals()方法必须是这样的,以便您可以将您的姓名与相应的提名名称进行比较,而不是直接与Name对象进行比较

public boolean equals(Name otherName)
       {  
          return (myFirstname.equalsIgnoreCase(otherName.firstName)) && (myMiddlename.equalsIgnoreCase(otherName.middleName)) && (myLastname.equalsIgnoreCase(otherName.lastName));
       }

答案 1 :(得分:0)

你的equals方法是比较Name类的属性和类本身,所以基本上比较苹果和香蕉。您可以将Equals方法重构为:

return (myFirstname.equalsIgnoreCase(otherName.FirstName)) && (myMiddlename.equalsIgnoreCase(otherName.MiddleName)) && (myLastname.equalsIgnoreCase(otherName.LastName));