创建一副纸牌
Creating a Deck of Cards
我似乎无法弄清楚如何制作这副简单的纸牌。如果有人可以举例说明 "your code goes here" 部分的内容,那将非常有帮助。
通过在以下主要方法中输入合适的代码代替注释,创建一副纸牌:
public class Card
{
private String mySuit;
private int myValue;
public Card( String suit, int value )
{
mySuit = suit;
myValue = value;
}
public String name()
{
String[] cardNames =
{
"Deuce", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Ten",
"Jack", "Queen", "King", "Ace"
};
return cardNames[ myValue - 2 ] + " of " + mySuit;
}
}
public class MainClass
{
public static void main( String[] args )
{
Card[] deck = new Card[ 52 ];
String[] suits = { "spades", "hearts", "diamonds", "clubs" };
int i;
for ( i = 0 ; i < suits.length ; i++ )
{
for ( int k = 2 ; k <= 14 ; k++ )
{
// your code goes here
}
}
for ( Card card : deck )
System.out.println( card.name() );
}
}
我没有时间对此进行测试,但我的意图是这样的:
deck[13 * i + k - 2] = new Card(suits[i], k);
尽管我个人也会对西装使用整数——只是浪费时间 space 使用字符串。
我似乎无法弄清楚如何制作这副简单的纸牌。如果有人可以举例说明 "your code goes here" 部分的内容,那将非常有帮助。
通过在以下主要方法中输入合适的代码代替注释,创建一副纸牌:
public class Card
{
private String mySuit;
private int myValue;
public Card( String suit, int value )
{
mySuit = suit;
myValue = value;
}
public String name()
{
String[] cardNames =
{
"Deuce", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Ten",
"Jack", "Queen", "King", "Ace"
};
return cardNames[ myValue - 2 ] + " of " + mySuit;
}
}
public class MainClass
{
public static void main( String[] args )
{
Card[] deck = new Card[ 52 ];
String[] suits = { "spades", "hearts", "diamonds", "clubs" };
int i;
for ( i = 0 ; i < suits.length ; i++ )
{
for ( int k = 2 ; k <= 14 ; k++ )
{
// your code goes here
}
}
for ( Card card : deck )
System.out.println( card.name() );
}
}
我没有时间对此进行测试,但我的意图是这样的:
deck[13 * i + k - 2] = new Card(suits[i], k);
尽管我个人也会对西装使用整数——只是浪费时间 space 使用字符串。