比较对象的两个返回值 [英] Compare two return values of object

查看:92
本文介绍了比较对象的两个返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何比较对象的两个返回值?因为当我在我的代码中执行此操作时,返回值每次都不同。这是我的代码:

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;   
        }

结果:
第一轮:
AceSpades
AceSpades
false

Result: 1st run: AceSpades AceSpades false

第二轮:
AceSpades
AceSpades
true

2nd run: AceSpades AceSpades true

推荐答案

在java(以及许多通过引用语言传递)中,==运算符会比较存储对象的内存位置。因为它们是两个不同的对象,所以它们存储在不同的内存位置。

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.

你需要使用这样的.equals()方法:

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

c1.equals(c2)

您需要编写班级的equals方法来检查卡片是否相等,在这种情况下,通过比较它们的编号和套装。

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.

这篇关于比较对象的两个返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆