使用面向对象的方式在Java中实现扑克牌游戏时遇到了空值问题。

huangapple go评论74阅读模式
英文:

Getting null with OOP Deck of Cards Java

问题

public class Card {
    String[] rank = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
    String suit;
    int value;
    String color;
    
    public Card(String suit, int value) {
        this.suit = suit;
        this.value = value;
        if (suit.equals("Hearts") || suit.equals("Diamonds")) {
            color = "red";
        } else {
            color = "black";
        }
    }
    
    public int getValue() {
        return value;
    }
    
    public String getColor() {
        return color;
    }
    
    public String getSuit() {
        return suit;
    }
    
    public String getName() {
        String name = rank[(value - 1)];
        return name;
    }
    
    public String toString() {
        return getName() + " of " + suit;
    }
}
public class Deck {
    String[] suits = {"Hearts", "Diamonds", "Clubs", "Spades"};
    static String[] cards = new String[52];
    
    public Deck() {
        int index = 0;
        for (int i = 0; i < 4; i++) {
            for (int j = 1; j <= 13; j++) {
                Card card = new Card(suits[i], j);
                cards[index] = card.toString(); // Fixed the assignment here
                index += 1;
            }
        }
    }
    
    public static void all() {
        for (int i = 0; i < cards.length; i++) {
            System.out.println(cards[i]);
        }
    }
}

In your Deck class, the issue causing the nulls is with the line cards[index] += card;. You should use cards[index] = card.toString(); instead to correctly assign the card's string representation to the cards array.

英文:

So I am building up to a card game but, but I am having some trouble building the deck of cards and OOP in general so I was wondering if I could get some help. This is what I have so far.

public class Card {
    String[] rank = {&quot;Ace&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;5&quot;, &quot;6&quot;,
            &quot;7&quot;, &quot;8&quot;, &quot;9&quot;, &quot;10&quot;, &quot;Jack&quot;, &quot;Queen&quot;, &quot;King&quot;};
    String suit;
    int value;
    String color;

    public Card(String suit, int value) {
        this.suit = suit;
        this.value = value;
        if (suit.equals(&quot;Hearts&quot;) || suit.equals(&quot;Diamonds&quot;)) {
            color = &quot;red&quot;;
        } else {
            color = &quot;black&quot;;
        }
    }

    public int getValue() {
        return value;
    }

    public String getColor() {
        return color;
    }

    public String getSuit() {
        return suit;

    }

    public String getName() {
        name = rank[(value - 1)];
        return name;

    }

    public String toString() {
        return name + &quot; of &quot; + suit;
    }
}

The class for my deck of cards (so far) is as follows:

    public class Deck {
    String[] suits = {&quot;Hearts&quot;, &quot;Diamonds&quot;, &quot;Clubs&quot;, &quot;Spades&quot;};
    static String[] cards = new String[52];

    public Deck() {
        int index = 0;
        for (int i = 0; i &lt; 4; i++) {
            // For each value of card with this suit.
            for (int j = 1; j &lt;= 13; j++) {
                Card card = new Card(suits[i], j);
                cards[index] += card;
                index += 1;
            }
        }
    }

    public static void all() {
        for (int i = 0; i &lt; cards.length; i++) {
            System.out.println(cards[i]);
        }
    }
}

So my question was when I print out the cards, I get something like

nullnull of Hearts
nullnull of Hearts
nullnull of Hearts
nullnull of Hearts....
nullnull of Diamonds
nullnull of Diamonds
nullnull of Diamonds
nullnull of Diamonds
nullnull of Diamonds... etc.

Why am I getting these nulls? What am I doing wrong? Also, any other tips to improve my code would be greatly appreciated.

答案1

得分: 1

你的问题出在 Deck 类的构造函数中的以下代码行。

cards[index] += card;

你需要移除 + 符号。

在 Java 中,对象数组总是被初始化为每个元素都是 null。因此,你正在将一个 card 追加到 null 上。

如果你使用调试器逐步运行代码,你会发现这个问题。所有好的集成开发环境(IDE)都有调试器。你应该学会使用它。

请注意,你提供的代码在问题上无法编译,但我找出了如何修复它,因为问题很小。

为了完整起见,这是我修复后的代码。你可以将它与你的代码进行比较。

Card

public class Card {
    String[] rank = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
    String suit;
    int value;
    String color;

    public Card(String suit, int value) {
        this.suit = suit;
        this.value = value;
        if (suit.equals("Hearts") || suit.equals("Diamonds")) {
            color = "red";
        }
        else {
            color = "black";
        }
    }

    public int getValue() {
        return value;
    }

    public String getColor() {
        return color;
    }

    public String getSuit() {
        return suit;
    }

    public String getName() {
        String name = rank[(value - 1)];
        return name;

    }

    public String toString() {
        return getName() + " of " + suit;
    }
}

Deck
(注意,我添加了一个 main() 方法仅用于测试目的。)

public class Deck {
    String[] suits = {"Hearts", "Diamonds", "Clubs", "Spades"};
    Card[] cards = new Card[52];

    public Deck() {
        int index = 0;
        for (int i = 0; i < 4; i++) {
            // For each value of card with this suit.
            for (int j = 1; j <= 13; j++) {
                Card card = new Card(suits[i], j);
                cards[index] = card;
                index += 1;
            }
        }
    }

    public void all() {
        for(int i = 0; i < cards.length; i++) {
            System.out.println(cards[i]);
        }
    }

    public static void main(String[] args) {
        new Deck().all();
    }
}
英文:

Your problem is the following line of code from the constructor of class Deck.

cards[index] += card;

You need to remove the +.

An array of objects in java is always initialized such that every element is null. Hence you are appending a card to null.

If you would step through your code with a debugger, you would have discovered that. All good IDEs have a debugger. You should learn to use it.

Note that the code in your question does not compile but I figured out how to fix it since the problems are minor.

For the sake of completeness. Here is your code with my fixes. Compare it to your code.

Class Card

public class Card {
    String[] rank = {&quot;Ace&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;5&quot;, &quot;6&quot;, &quot;7&quot;, &quot;8&quot;, &quot;9&quot;, &quot;10&quot;, &quot;Jack&quot;, &quot;Queen&quot;, &quot;King&quot;};
    String suit;
    int value;
    String color;

    public Card(String suit, int value) {
        this.suit = suit;
        this.value = value;
        if (suit.equals(&quot;Hearts&quot;) || suit.equals(&quot;Diamonds&quot;)) {
            color = &quot;red&quot;;
        }
        else {
            color = &quot;black&quot;;
        }
    }

    public int getValue() {
        return value;
    }

    public String getColor() {
        return color;
    }

    public String getSuit() {
        return suit;
    }

    public String getName() {
        String name = rank[(value - 1)];
        return name;

    }

    public String toString() {
        return getName() + &quot; of &quot; + suit;
    }
}

Class Deck
(Note that I added a main() method just for testing purposes.)

public class Deck {
    String[] suits = {&quot;Hearts&quot;, &quot;Diamonds&quot;, &quot;Clubs&quot;, &quot;Spades&quot;};
    Card[] cards = new Card[52];

    public Deck() {
        int index = 0;
        for (int i = 0; i &lt; 4; i++) {
            // For each value of card with this suit.
            for (int j = 1; j &lt;= 13; j++) {
                Card card = new Card(suits[i], j);
                cards[index] = card;
                index += 1;
            }
        }
    }

    public void all() {
        for(int i = 0; i &lt; cards.length; i++) {
            System.out.println(cards[i]);
        }
    }

    public static void main(String[] args) {
        new Deck().all();
    }
}

答案2

得分: 0

你的示例看起来不完整,因为在 Card 类中我没有看到 name 字段的定义。但是我看到 namegetName 方法中被初始化。我猜你没有进行初始化。可以在 toString 方法中使用 getName

public String toString(){
    return getName() + " of " + suit;
}

或者在构造函数中进行 name 的初始化:name = rank[(value - 1)]

最好在构造函数中进行初始化,否则如果在其他地方引用了 name,可能会遇到相同的问题。

英文:

Your example does not look complete, as I don't see name field in Card class defined. But what I see is that name is initialized in getName methods. I guess you don't initialize it.
Either use getName in toString:

public String toString(){
    return getName() + &quot; of &quot; + suit;
}

or initialize name in the constructor: name = rank[(value - 1)]

Prefer initialization in the constructor, otherwise if you refer to name in some other place you can get the same issue.

huangapple
  • 本文由 发表于 2020年10月18日 22:20:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/64414384.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定