【问题标题】:Arrays of cards not showing any output没有显示任何输出的卡片数组
【发布时间】:2020-08-20 20:33:51
【问题描述】:

为此,我创建了两个 java 文件。 Card.java 具有所有实例变量Jack.java 会在被询问时运行所有card

我的 Card.java 代码是:

public class Card {

    private int rank;
    private int suit;


    public Card(int rank, int suit) {

        this.rank = rank;
        this.suit = suit;
    }

    public static final String[] RANKS = {
        null, "Ace", "2", "3", "4", "5", "6", "7",
        "8", "9", "10", "Jack", "Queen", "King"};
    public static final String[] SUITS = {
        "Clubs", "Diamonds", "Hearts", "Spades"};

    public String toString() {
        String s = RANKS[this.rank] + " of " + SUITS[this.suit]; // we are actually using the instances variables to access the array elements 
        return s;

    }

    public boolean equals(Card that) { // this that could be any variables 
        return this.rank == that.rank && this.suit == that.suit;
    }
}

我的 Jack.java 代码是:

public class Jack {

    public static void main(String[] args) {

        Card small = new Card(0,1); // creates the small object
        Card card = new Card(11,1); // cretes the card object
        System.out.println(small); // prints the small class
        System.out.println(card); // prints the card class
        System.out.println(small.equals(card)); // compares small with cards

        Card[] cards = new Card[52];

        int index = 0; // to keep track
        for (int suit = 0; suit <= 3; suit++) { // outer loop
            for (int rank = 1; rank <= 13; rank++) { // inner loop
                cards[index] = new Card(rank, suit);
                index++;
            }
        }

        public static void printDeck(Card[] cards) {
            for (Card card : cards) {
                System.out.println(card);
            }
        }   

        System.out.println(Arrays.toString(cards));
    }  
}

所有代码都有效,但 PrintArray 无效。我试图从书中获得帮助,但我没有得到可能的错误。提前致谢。我还尝试将 void 方法 printDeck 放在 Card.java 文件中,但又失败了。

【问题讨论】:

  • 当你说它不起作用时 - 为什么?你能详细解释一下这个问题吗?
  • 代码无效 - 如果你愿意,你可以在代码级别有一个方法 printDeck - 它需要是一个类级别的项目。

标签: java class variables instance


【解决方案1】:

问题:你的常量不是。

拥有公共数组是个坏主意。您将final 卡在那里的事实并不能解决这个问题;数组在这里不能做你想做的事。我建议您改用列表。试试吧!写这个:Card.RANKS[1] = "10"; // hahaha I'm cheating!

试试这个:

public static final List<String> RANKS = List.of("", "Ace", "2", "3", ...);

或者更好的是,按照它的本意使用 java。带有类型和名称:

public enum Rank {
    ACE, R2, R3, R4, R5, R6, R7, R8, R9, R10, JACK, QUEEN, KING;
}

public class Card {
    private final Rank rank;
}

问题:你的 equals 不是。

java 的工作方式是运行时方法的名称比名称要复杂一些。它实际上是名称,是所有参数的所有类型,但没有任何泛型,是返回类型。所以,equals的全称其实是equals(java.lang.Object)boolean。您的 equals 方法 DOES NOT 覆盖,因为它实际上没有相同的名称;它的名字是equals(Card)boolean。不一样。对于任何与 equals 方法交互的代码(例如 hashmap、hashset、list.contains 等),它都不起作用。你应该让你的 equals 方法接受一个Object,并检查它是否是一张卡片;如果不是,显然,它是不相等的。所以,添加:if (!(that instanceof Card)) return false;。如果卡不是最终的,更好的检查是if (that == null || that.getClass() != Card.class) return false;,原因有些复杂。

@Override 添加到您认为应该覆盖的任何方法中是个好主意。这什么都不做,除非你的方法实际上没有覆盖任何东西,在这种情况下,它会导致编译器告诉你这个:嘿,伙计 - 你的假设不成立 - 然后拒绝编译你的代码以便你可以修复它。

问题:null 在那里做什么?

您的 RANKS 以 null 开头,但您创建了 [A] 不可读的 new Card(0,1);,而 [B] 表示您现在有一个 null 等级。不需要那个空值。此外,如果您使用枚举和类型,它可以变得更易读:new Card(Rank.ACE, Suit.DIAMONDS);

打印不工作

您已将方法放入方法中(printDeck 方法位于 main 方法中)。 Java(当前)不支持这个。移动方法,使其成为兄弟。

【讨论】:

    【解决方案2】:

    您的代码不会按原样编译。 printDeck() 方法目前在 main() 方法的定义内,需要移到外面。移动方法后,它按预期运行。这是修改后的 Jack 类:

    public class Jack {
    
        public static void main(String[] args) {
    
            Card small = new Card(0,1); // creates the small object
            Card card = new Card(11,1); // cretes the card object
            System.out.println(small); // prints the small class
            System.out.println(card); // prints the card class
            System.out.println(small.equals(card)); // compares small with cards
    
            Card[] cards = new Card[52];
    
            int index = 0; // to keep track
            for (int suit = 0; suit <= 3; suit++) { // outer loop
                for (int rank = 1; rank <= 13; rank++) { // inner loop
                    cards[index] = new Card(rank, suit);
                    index++;
                }
            }
           printDeck(cards);
        }
    
        public static void printDeck(Card[] cards) {
            for (Card card : cards) {
                System.out.println(card);
            }
        }
    }
    

    结果输出:

    null of Diamonds
    Jack of Diamonds
    false
    Ace of Clubs
    2 of Clubs
    3 of Clubs
    4 of Clubs
    5 of Clubs
    6 of Clubs
    7 of Clubs
    8 of Clubs
    9 of Clubs
    10 of Clubs
    Jack of Clubs
    Queen of Clubs
    King of Clubs
    Ace of Diamonds
    2 of Diamonds
    3 of Diamonds
    4 of Diamonds
    5 of Diamonds
    6 of Diamonds
    7 of Diamonds
    8 of Diamonds
    9 of Diamonds
    10 of Diamonds
    Jack of Diamonds
    Queen of Diamonds
    King of Diamonds
    Ace of Hearts
    2 of Hearts
    3 of Hearts
    4 of Hearts
    5 of Hearts
    6 of Hearts
    7 of Hearts
    8 of Hearts
    9 of Hearts
    10 of Hearts
    Jack of Hearts
    Queen of Hearts
    King of Hearts
    Ace of Spades
    2 of Spades
    3 of Spades
    4 of Spades
    5 of Spades
    6 of Spades
    7 of Spades
    8 of Spades
    9 of Spades
    10 of Spades
    Jack of Spades
    Queen of Spades
    King of Spades
    

    【讨论】:

      猜你喜欢
      • 2023-03-03
      • 2016-08-08
      • 1970-01-01
      • 1970-01-01
      • 2018-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-24
      相关资源
      最近更新 更多