so I'm supposed to create accessor methods getSuit and getValue for the mySuit and myValue fields, respectively. Then I also need to make a method imageFileName that returns the filename of the image that will be used when I display the card in a graphics window.

所以我应该分别为mySuit和myValue字段创建访问器方法getSuit和getValue。然后我还需要创建一个方法imageFileName,它返回在图形窗口中显示卡片时将使用的图像文件名。

The filenames are strings that use only numbers, lower case letters, underscore characters, and a period. They all end in ".png". Here are some examples:

文件名是仅使用数字,小写字母,下划线字符和句点的字符串。它们都以“.png”结尾。这里有些例子:

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

Card 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

}

Main Method

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

and the expected result is:

并且预期的结果是:

Value: 10

Suit: diamonds

Filename: 10_of_diamonds.png

If somebody could please explain what to use in the imageFileName method, I'd be very grateful. Thank you!

如果有人可以解释在imageFileName方法中使用什么,我将非常感激。谢谢!

2 个解决方案

#1


2

So you want to concatenate the strings, adding underscores and "of"s? Have a look at the Java String tutorial, search for "java string concatenation" on the web and here on SO.

所以你想连接字符串,添加下划线和“of”?看看Java String教程,在网上搜索“java string concatenation”,在这里搜索SO。

To give you some code to play with as well:

为了给你一些代码可以玩:

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

更多相关文章

  1. java 和 JavaScript都可以在创建一个对象时,就可以通过这个对象调
  2. 有什么方法可以避免HibernateOptimisticLockingFailureException
  3. native方法的使用
  4. Java Quartz的使用方法,实现程序计时
  5. 将行计数器方法与字数统计方法相结合
  6. java基础:集合框架之Map(共性方法)
  7. JAVA中的反射只获取属性的get方法
  8. 再论javaIO之拷贝MP3(read方法返回int的原因)
  9. java基础-Arrays类常用方法介绍

随机推荐

  1. Webview实现Android和JS通信
  2. CAMERA(12)---[Android相机]光线传感器识
  3. android 那些事---主线程是线程不安全的
  4. android json 转换
  5. android inputType属性
  6. Android(Java):把EHCache集成到android项
  7. 搭建Android UI Testing自动化测试开发环
  8. Android系列之Android(安卓)命令行手动编
  9. Android的线程
  10. Android init.rc文件解析过程分析