Compare two return values of object [duplicate]

2019-03-06 20:22发布

问题:

This question already has an answer here:

  • Compare two return values [duplicate] 1 answer

How do I compare two return values of an object? Because when I'm doing this in my code and the return values is different each time. Here is my code:

public static void Card_Initialization(){

 Red_Dog c1 = new Red_Dog();
 Red_Dog c2 = new Red_Dog();
 Cards_Match(c1);
 System.out.println(card_num+card_suit);
 Cards_Match(c2);
 System.out.println(card_num+card_suit);
 System.out.println(Cards_Match(c1) == Cards_Match(c2));//to check really if it is equal
 }

        public static int Cards_Match(Red_Dog rd){
            card = (int)(Math.random() * deck.length); 
             if(card >= 0 && card <=3)
         {
            card_num = cards[0];

         }
         else if(card >= 4 && card <=7)
         {
            card_num = cards[1];
         }
         else if(card >= 8 && card <=11)
         {
            card_num = cards[2];
         }
         else if(card >= 12 && card <=15)
         {
            card_num = cards[3];
         }
         else if(card >= 16 && card <=19)
         {
            card_num = cards[4];
         }
         else if(card >= 20 && card <=23)
         {
            card_num = cards[5];
         }
         else if(card >= 24 && card <=27)
         {
            card_num = cards[6];
         }
         else if(card >= 28 && card <=31)
         {
            card_num = cards[7];
         }
         else if(card >= 32 && card <=35)
         {
            card_num = cards[8];
         }
         else if(card >= 36 && card <=39)
         {
            card_num = cards[9];
         }
         else if(card >= 40 && card <=43)
         {
            card_num = cards[10];
         }
         else if(card >= 44 && card <=47)
         {
            card_num = cards[11];
         }
         else if(card >= 48 && card <=51)
         {
            card_num = cards[12];
         }

         if(card % 4 == 0)
         {
            card_suit = suits[0];
         }

         else if(card % 4 == 1)
         {
            card_suit = suits[1];
         }

         else if(card % 4 == 2)
         {
            card_suit = suits[2];
         }

         else if(card % 4 == 3)
         {
            card_suit = suits[3];
         }

         return card;   
        }

Result: 1st run: AceSpades AceSpades false

2nd run: AceSpades AceSpades true

回答1:

In java (and many pass by reference languages), the == operator compares the memory location where the objects are stored. Because these are two different objects, they are stored in different memory locations.

You need to use a .equals() method like this:

c1.equals(c2)

You will need to write the equals method of your class to check if the cards equal each other, in this case by comparing both their number and suit.



回答2:

I believe you should change this :

 System.out.println(Cards_Match(c1) == Cards_Match(c2));

to

 System.out.println(c1.equals(c2));


回答3:

Your naming got us confused. Cards_Match is not an object is a method!

A method that generates random numbers depending on objects that you have not included in your question. If you compare generated random int numbers, you are going to obtain different results each time.

I think that you are trying to implement: pick a card 1 randomly, pick another card 2 randomly and then compare then. But it's not working with the rest of your program.

Please use java naming conventions in the future. http://www.oracle.com/technetwork/java/codeconventions-135099.html



标签: java oop