gpt4 book ai didi

java - 创建一个类来代表一张扑克牌

转载 作者:行者123 更新时间:2023-12-01 18:43:38 24 4
gpt4 key购买 nike

我正在尝试创建一个 Card 类来代表扑克牌。底部的代码运行顶部的代码。我只需要入门帮助。我不太确定该怎么做。我如何为这个类创建构造函数?

public class Card
{
public static final String FACES[] = {"ZERO","ACE","TWO","THREE","FOUR",
"FIVE","SIX","SEVEN","EIGHT","NINE","TEN","JACK","QUEEN","KING"};

//instance variables
//String suit
//int face

//constructors


// modifiers
//set methods


//accessors
//get methods


//toString

}
<小时/>
public class CardRunner
{
public static void main( String args[] )
{
Card one = new Card("SPADES", 9);
out.println(one.getSuit());
out.println(one.getFace());

Card two = new Card("DIAMONDS", 1);
out.println(two);
two.setFace(3);
out.println(two);

Card three = new Card("CLUBS", 4);
out.println(three);

Card four = new Card("SPADES", 12);
out.println(four);

Card five = new Card("HEARTS", 12);
out.println(five);
}
}

最佳答案

我认为你应该使用enum这里是PlayingCard因为所有都是常量,您可以覆盖 toString给出有意义的表示。

如果创建枚举,则不必显式创建对象,并且可以在需要时使用枚举常量。您可以阅读 why to use enumerations in java 上的精彩教程。

关于java - 创建一个类来代表一张扑克牌,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18873001/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com