所以我应该分别为mySuit和myValue字段创建访问器方法getSuit和getValue。然后我还需要创建一个方法imageFileName,它返回在图形窗口中显示卡片时将使用的图像文件名。
文件名是仅使用数字,小写字母,下划线字符和句点的字符串。它们都以“.png”结尾。以下是一些例子:
"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方法中使用什么,我将非常感激。谢谢!
答案 0 :(得分:2)
所以你想要concatenate字符串,添加下划线和"" s?看看Java String tutorial,搜索" java字符串连接"在网上,在这里SO。
为您提供一些代码:
return myValue + "_of_" + mySuit.toLowerCase() + ".png";
答案 1 :(得分:0)
试试这个
return myValue + "_of_" + mySuit.toLowerCase();
(实际上如果套装总是像你的“钻石”一样小写,你根本不需要.toLowerCase()