Java类卡枚举示例。修订 [英] Java class card enum example. REVISED

查看:107
本文介绍了Java类卡枚举示例。修订的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

*非常感谢任何帮助*

*Any help is much appreciated *

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

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

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..

对于西装,我想做的是分配Heart = 4
diamond = 3,club = 2, spade = 1

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

排名,ace = 11
jack,queen,king = 10

2-10卡的价值..

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

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

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

所以我希望它打印八个黑桃(8),十个心(40)

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

基于从值...示例spade = 1 * 8
hearts = 4 * 10

based off from the values.. example spade = 1 * 8 hearts = 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), JACK(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
    }

}

这是主要的

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;
    }
}

当我尝试编译时收到错误..
for

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

card.getSuitpoints()

card.getSuitpoints()

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

error: cannot find symbol: method getSuitpoints()

我发现这很奇怪,因为

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

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

getRankpoints()
返回零。
什么错了?

getRankpoints() returns zero. whats wrong?

推荐答案

你在套牌中看到重复的原因是因为你在卡片上重复两次。

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, Card might be a better choice of name than Cards for a class that represents a card.

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

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.

这篇关于Java类卡枚举示例。修订的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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