发扑克牌的程序,不知道应该怎么发牌啊
这个练习的要求就是写出一个程序,发出5张扑克牌,并且打印出这5张牌是什么。这个练习要求:
Class Card 里的东西不能被改,只能改Deck的代码,CardMain的代码要自己写。
Deck的getCard,fill和toString我都不知道该怎么写。
getCard里面我写了一些,也不知道这么写对不对。
fill和toString都不知道该怎么写。
Deck里面的toString必须要呼叫Card里面的toString才行。
我写的这个程序打印出来就变成:
The five card you have got are: Jack of spades
The five card you have got are: Jack of spades
The five card you have got are: Jack of spades
The five card you have got are: Jack of spades
The five card you have got are: Jack of spades
求哪位高手能帮帮我啊,告诉我些具体的思路就行。
下面是代码:
程序代码:
package lab02;
public class CardMain
{
public static void main(String[] args)
{
Deck d = new Deck();
Card c = new Card(11, 3);
d.getCard();
for( int i = 0; i < 5; i++)
{
System.out.println("The five card you have got are: "
+ c.toString());
d.shuffleCards();
}
}
}
程序代码:
package lab02;
/** Objects of this class represents cards in
* a deck (of cards).
* A card is immutable, i.e. once created its
* rank or suit cannot be changed.
*/
public class Card
{
/** rank: 1 = Ace, 2 = 2, ...
* suit: 1 = spades, 2 = hearts, ...
*/
public Card(int rank, int suit)
{
this.rank = rank;
this.suit = suit;
}
public int getRank()
{
return rank;
}
public int getSuit()
{
return suit;
}
public String toString()
{
String info = rankTab[rank-1] + " of " + suitTab[suit-1];
return info;
}
private int rank, suit;
// Tables for converting rank & suit to text (why static?)
private static final String[] rankTab = {
"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"Jack", "Queen", "King"
};
private static final String[] suitTab = {
"clubs", "diamonds", "spades", "hearts"
};
}
程序代码:
package lab02;
import java.util.*;
public class Deck
{
private Card[] theCards;
private int noOfCards;
public Deck()
{
theCards = new Card[52];
noOfCards = 52;
this.fill();
}
public int getNoOfCards()
{
return noOfCards;
}
public Card getCard()
{
Card a = null;
a = theCards[noOfCards-1];
noOfCards--;
return a;
}
public void shuffleCards()
{
Random random = new Random(); // Bör deklareras static i Deck-klassen
Card temp;
int pos1, pos2; // Two random positions
for(int i = 0; i < 30; i++)
{
pos1 = random.nextInt(noOfCards);
pos2 = random.nextInt(noOfCards);
// Swap
temp = theCards[pos1];
theCards[pos1] = theCards[pos2];
theCards[pos2] = temp;
}
}
private void fill()
{
}
}






