在扑克游戏中显示纸牌图像

Displaying the Image for Cards in a Poker Game

所以我应该分别为 mySuit 和 myValue 字段创建访问器方法 getSuit 和 getValue。然后我还需要创建一个方法 imageFileName ,该方法 returns 当我在图形 window.

中显示卡片时将使用的图像的文件名

文件名是仅包含数字、小写字母、下划线字符和句点的字符串。它们都以“.png”结尾。以下是一些示例:

"2_of_diamonds.png" 
"10_of_clubs.png"
"ace_of_spades.png"
"queen_of_hearts.png"

卡片Class

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();

(实际上,如果花色总是像你的 "diamonds" 一样小写,你根本不需要 .toLowerCase()

所以你想 concatenate the strings, adding underscores and "of"s? Have a look at the Java String tutorial,在网上搜索 "java string concatenation",然后在此处搜索。

也给你一些代码来玩:

return myValue + "_of_" + mySuit.toLowerCase() + ".png";