This question already has an answer here:
这个问题在这里已有答案:
- Compare two return values [duplicate] 1 answer
比较两个返回值[重复] 1个答案
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
结果:第一次运行:AceSpades AceSpades为假
2nd run: AceSpades AceSpades true
第二轮:AceSpades AceSpades是真的
3 个解决方案
#1
3
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.
在java(以及许多通过引用语言传递)中,==运算符比较存储对象的内存位置。因为它们是两个不同的对象,所以它们存储在不同的存储位置。
You need to use a .equals() method like this:
你需要使用像这样的.equals()方法:
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.
您需要编写班级的equals方法来检查卡片是否相等,在这种情况下,通过比较它们的数量和套装。
#2
0
I believe you should change this :
我相信你应该改变这个:
System.out.println(Cards_Match(c1) == Cards_Match(c2));
to
System.out.println(c1.equals(c2));
#3
0
Your naming got us confused. Cards_Match is not an object is a method!
你的命名让我们感到困惑。 Cards_Match不是一个对象是一种方法!
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.
一种根据您未在问题中包含的对象生成随机数的方法。如果比较生成的随机int数,则每次都会获得不同的结果。
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命名约定。 http://www.oracle.com/technetwork/java/codeconventions-135099.html
#1
3
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.
在java(以及许多通过引用语言传递)中,==运算符比较存储对象的内存位置。因为它们是两个不同的对象,所以它们存储在不同的存储位置。
You need to use a .equals() method like this:
你需要使用像这样的.equals()方法:
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.
您需要编写班级的equals方法来检查卡片是否相等,在这种情况下,通过比较它们的数量和套装。
#2
0
I believe you should change this :
我相信你应该改变这个:
System.out.println(Cards_Match(c1) == Cards_Match(c2));
to
System.out.println(c1.equals(c2));
#3
0
Your naming got us confused. Cards_Match is not an object is a method!
你的命名让我们感到困惑。 Cards_Match不是一个对象是一种方法!
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.
一种根据您未在问题中包含的对象生成随机数的方法。如果比较生成的随机int数,则每次都会获得不同的结果。
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命名约定。 http://www.oracle.com/technetwork/java/codeconventions-135099.html