Java 类卡枚举示例。修改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2151232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:42:58  来源:igfitidea点击:

Java class card enum example. REVISED

java

提问by Steller

*Any help is much appreciated *

*任何帮助深表感谢 *

I am using the class card example from java website to try to build a game.

我正在使用 java 网站上的类卡示例来尝试构建游戏。

http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html

http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html

I want to assign the suits and ranks values. I am not sure how to do that..

我想分配西装和排名值。我不知道该怎么做..

For suit, What i want to do is assign Heart = 4 diamond = 3, club =2, spade = 1

对于西装,我想要做的是分配 Heart = 4 钻石 = 3,俱乐部 =2,黑桃 = 1

for rank, ace = 11 Hyman,queen,king = 10
2-10 the value of the card..

对于等级,ace = 11 Hyman,queen,king = 10
2-10 卡的价值..

the program accepts user input as arguments for number of hands and number of cards per hand.. like this: $ java Deal 4 5

该程序接受用户输入作为手数和每手牌数的参数..像这样:$ java Deal 4 5

so then I want it to print Eight of spades(8), ten of hearts(40)

那么我希望它打印黑桃八(8),红心十(40)

based off from the values.. example spade = 1 * 8 hearts = 4 * 10

基于值...例如黑桃 = 1 * 8 心 = 4 * 10

I can get it to print the hand just not the values...

我可以让它打印手而不是值...

Example of my Current output scaled down:

我的当前输出按比例缩小的示例:

FIVE of CLUBS(0),
DEUCE of SPADES(0),
SEVEN of SPADES(0),
TEN of SPADES(0),
THREE of HEARTS(0),
FOUR of SPADES(0),
THREE of DIAMONDS(0),
[TEN of SPADES, THREE of HEARTS, FOUR of SPADES, THREE of DIAMONDS]
[FOUR of CLUBS, FIVE of CLUBS, DEUCE of SPADES, SEVEN of SPADES]
[QUEEN of HEARTS, SIX of HEARTS, FOUR of HEARTS, KING of DIAMONDS]

C:\Java\a02>

Here is the code for the program in two different classes

这是两个不同类中的程序代码

import java.util.*;

public class Cards {

    public enum Rank {
        DEUCE(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(
                9), TEN(10), Hyman(10), QUEEN(10), KING(10), ACE(11);

        private int Rankpoints;

        Rank(int points) {
            this.Rankpoints = points;
        }

        public int getRankpoints() {
            return this.Rankpoints;
        }

    }

    public enum Suit {
        CLUBS(2), DIAMONDS(3), HEARTS(4), SPADES(1);

        private int Suitpoints;

        Suit(int points) {

            this.Suitpoints = points;

        }

        public int getSuitpoints() {
            return this.Suitpoints;
        }

    }

    private final Rank rank;
    private final Suit suit;

    private Cards(Rank rank, Suit suit) {
        this.rank = rank;
        this.suit = suit;
    }

    public Rank rank() {
        return this.rank;
    }

    public Suit suit() {

        return this.suit;

    }

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

    private static final List<Cards> protoDeck = new ArrayList<Cards>();

    // Initialize prototype deck
    static {
        for (Suit suit : Suit.values())
            for (Rank rank : Rank.values())
                protoDeck.add(new Cards(rank, suit));

    }

    public static ArrayList<Cards> newDeck() {

        return new ArrayList<Cards>(protoDeck); // Return copy of prototype deck
    }

}

and here is the main

这是主要的

import java.util.*;

public class Deal {
    public static void main(String args[]) {
        int numHands = Integer.parseInt(args[0]);
        int cardsPerHand = Integer.parseInt(args[1]);
        List<Cards> deck = Cards.newDeck();
        Collections.shuffle(deck);

        for (Cards card : deck) {
            System.out.println(card.rank() + " of " + card.suit() + "("
                    + card.getSuitpoints() + ")" + ",");

        }

        for (int i = 0; i < numHands; i++)
            System.out.println(deal(deck, cardsPerHand));
    }

    public static ArrayList<Cards> deal(List<Cards> deck, int n) {
        int deckSize = deck.size();
        List<Cards> handView = deck.subList(deckSize - n, deckSize);
        ArrayList<Cards> hand = new ArrayList<Cards>(handView);
        handView.clear();
        return hand;
    }
}

when i try to compile i get an error.. for

当我尝试编译时出现错误.. for

card.getSuitpoints()

card.getSuitpoints()

error: cannot find symbol: method getSuitpoints()

错误:找不到符号:方法 getSuitpoints()

i find that odd because

我觉得这很奇怪,因为

card.getRankpoints() compiles.. is it the way i am putting it into the enum?

card.getRankpoints() 编译..这是我将它放入枚举的方式吗?

getRankpoints() returns zero. whats wrong?

getRankpoints() 返回零。怎么了?

回答by danben

The reason you are seeing duplicates in your deck is because you are iterating over the cards twice.

您在牌组中看到重复的原因是因为您对卡片进行了两次迭代。

for (Cards suit : deck) // ********* here is where i print the deck ********
{
    for (Cards rank : deck) {
        System.out.println(rank.rank() + " of " + suit.suit() + ",");
    }
}

Assuming your deck generator works, it would be sufficient to iterate once:

假设你的牌组生成器工作,迭代一次就足够了:

for (Cards card : deck)
{ 
    System.out.println(card.rank() + " of " +  card.suit() + ",");  
} 

Also, Cardmight be a better choice of name than Cardsfor a class that represents a card.

此外,与代表卡片的类Card相比,可能是更好的名称选择Cards

To get the value, you need to add a method that returns it.

要获取该值,您需要添加一个返回它的方法。

Rank(int points)
{
    this.Rankpoints = points;  
}

public int getRankpoints() {
    return this.Rankpoints;
}

Then you can call that when you want to print the value.

然后你可以在你想要打印值时调用它。