比较两个返回值 [英] Compare two return values

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

问题描述

如何比较我的方法中的两个返回值?因为当我在我的代码中执行此操作并且我期望返回值相同时,它们会进行不同的比较。这是我的代码:

How do I compare two return values from my method? Because when I'm doing this in my code and I'm expecting the return values to be the same, they are comparing as different. 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

AceSpades AceSpades false

第二轮:


AceSpades AceSpades true

AceSpades AceSpades true


推荐答案

而不是传递RedDod对象然后忽略,我建议你使方法非静态,只设置该类的字段。

Instead of passing the RedDod object and then ignoring, I suggest you make the method non-static and only set fields of that class.

import java.util.Random;

public class RedDog implements Comparable<RedDog> {
    private final Suit suit;
    private final Face face;

    enum Suit {
        CLUB, SPADES, DIAMONDS, HEARTS
    }
    static final Suit[] SUITS = Suit.values();

    enum Face {
        ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING
    }
    static final Face[] FACES = Face.values();

    static final Random RAND = new Random();

    public RedDog(Suit suit, Face face) {
        this.suit = suit;
        this.face = face;
    }

    public static RedDog random() {
        return new RedDog(SUITS[RAND.nextInt(SUITS.length)], 
                          FACES[RAND.nextInt(FACES.length)]);
    }

    public static void main(String[] args) {
        RedDog c1 = RedDog.random();
        RedDog c2 = RedDog.random();
        System.out.println(c1 + " and " + c2 + " equals is " + c1.equals(c2));
    }

    @Override
    public boolean equals(Object o) { // generated by my IDE
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        RedDog redDog = (RedDog) o;

        if (suit != redDog.suit) return false;
        return face == redDog.face;

    }

    @Override
    public int hashCode() { // generated by my IDE
        int result = suit != null ? suit.hashCode() : 0;
        result = 31 * result + (face != null ? face.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() { // generated by my IDE
        return "RedDog{" +
                "face=" + face +
                ", suit=" + suit +
                '}';
    }

    @Override
    public int compareTo(RedDog r) {
        int cmp = suit.compareTo(r.suit);
        if (cmp == 0)
            cmp = rank.compareTo(r.rank);
        return cmp;
    }
}

打印

RedDog{face=KING, suit=SPADES} and RedDog{face=QUEEN, suit=DIAMONDS} equals is false

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

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