Java 在扑克游戏中显示牌的图像
所以我应该分别为mySuit和myValue字段创建访问器方法getSuit和getValue。然后,我还需要创建一个imageFileName方法,该方法返回在图形窗口中显示卡时将使用的图像的文件名 文件名是仅使用数字、小写字母、下划线字符和句点的字符串。它们都以“.png”结尾。以下是一些例子:Java 在扑克游戏中显示牌的图像,java,object,png,Java,Object,Png,所以我应该分别为mySuit和myValue字段创建访问器方法getSuit和getValue。然后,我还需要创建一个imageFileName方法,该方法返回在图形窗口中显示卡时将使用的图像的文件名 文件名是仅使用数字、小写字母、下划线字符和句点的字符串。它们都以“.png”结尾。以下是一些例子: "2_of_diamonds.png" "10_of_clubs.png" "ace_of_spades.png" "queen_of_hearts.png" 卡类 public class C
"2_of_diamonds.png"
"10_of_clubs.png"
"ace_of_spades.png"
"queen_of_hearts.png"
卡类
public class Card
{
/** This card's suit */
private String mySuit;
/** This card's pip value (aces high) */
private int myValue;
/** The English names of the cards in a suit */
private String[] cardNames =
{
"Deuce", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"
};
/**
* The class constructor
*
* @param suit A String, either "spades", "hearts", "diamonds", or "clubs"
* @param value An int from 2 through 14
*/
public Card( String suit, int value )
{
mySuit = suit;
myValue = value;
}
/**
* Gets the full English name of this card
*
* @return the full name of this card in English
*/
public String name()
{
return cardNames[ myValue - 2 ] + " of " + mySuit;
}
public String getSuit()
{
return this.suit;
}
public int getValue()
{
return this.value;
}
/**
* Gets the filename of this card's image
*
* @return the filename of this card's image
*/
public String imageFileName()
{
return ???.toLowerCase(); // This is the part I'm stuck on
}
主要方法
public static void main( String[] args )
{
Card c = new Card( "diamonds", 10 );
System.out.println( "Value: " + c.getValue() );
System.out.println( "Suit: " + c.getSuit() );
System.out.println( "Filename: " + c.imageFileName() );
}
预期结果是:
数值:10
西装:钻石
文件名:10_of_diamonds.png
如果有人能解释在imageFileName方法中使用什么,我将非常感激。谢谢大家! 试试这个
return myValue + "_of_" + mySuit.toLowerCase();
(事实上,如果西装总是小写的,就像你的“钻石”,你根本不需要.toLowerCase()
)那么你想在字符串中添加下划线和“of”s?看一看,在web上搜索“java字符串连接”等等
要同时提供一些代码供您使用,请执行以下操作:
return myValue + "_of_" + mySuit.toLowerCase() + ".png";
我想,你可以用铅笔和纸找出解决这个问题的逻辑。谢谢你的帮助,也谢谢你的链接,我刚刚读完。