我正在使用java进行视频扑克任务。我们给出了我们应该在我们的程序中使用的类模板。我对Card c
所代表的内容感到困惑。与方法addCard(Card c)
一样,这是否意味着它将在当前正在使用的Card对象上执行?以下是我目前一直在处理的类文件。它们还远没有完成。
玩家等级:
import java.util.ArrayList;
public class Player {
private ArrayList<Card> hand; // the player's cards
// you will likely need more instance variables
public Player(){
ArrayList<Card> hand = new ArrayList;
}
public void addCard(Card c){
// add the card c to the player's hand
}
public void removeCard(Card c){
// remove the card c from the player's hand
}
// you will likely need more methods here
}
甲板课程:
import java.util.Random
public class Deck {
private Card[] theDeck;
private int top;
// add more instance variables if needed
public Deck(){
top = 0
Card[] theDeck = new Card[52];
for(int s = 1; s <= 4; s++)
{
for (int v = 1; v <= 13; v++)
{
for (int i = 0; i < theDeck.length; i++)
theDeck[i] = new Card(s,v);
}
public void shuffle()
{
// shuffle the deck here
Random generator = new Random();
int i;
int j;
int temp = i;
int i = generator.nextInt(51) + 1;
int j = generator.nextInt(51) + 1;
for(int k = 1; k <100; k++)
{
theDeck[i] = theDeck[j];
theDeck[j] = temp;
}
top = 0;
}
public Card deal(){
// deal the top card in the deck
if (top < 40)
{
theDeck.shuffle;
}
return theDeck[top];
top++;
} //this method should add cards to array hand? no addCard will do that.
// add more methods here if needed
//DB getter methods here?
}
卡类:
public class Card implements Comparable<Card>{
private int suit;
private int value;
public Card(int s, int v){ //constructor of an object Card
s = suit;
v = value;
//make a card with suit s and value v
}
public int compareTo(Card c){
// use this method to compare cards so they
// may be easily sorted
}
public String toString() //to tell the user what card/s they have
{
myCard.getSuit();
myCard.getValue();
if(s == 1)
{
if(v == 11)
{
return "Jack of Clubs";
}
if(v == 12)
{
return "Queen of Clubs";
}
if(v == 13)
{
return "King of Clubs";
}
if(v == 1)
{
return "Ace of Clubs";
}
else{
return v + " of Clubs";
}
}
if(s == 2)
{
return v + "Diamonds";
}
if(s == 3)
}
return v + "Hearts";
}
if(s == 4)
}
return v + "Spades";
}
//DB method to set 1, 2, 3, and 4 to card suits
//now here create string representation for the Card
// use this method to easily print a Card object
public int getSuit()
{
return s;
}
public int getValue()
{
return v;
}
//DB right now have cards in theDeck like Card(2, 10), need Card(d, 10)
//need to convert that to a String d10
}
// add some more methods here if needed
}
答案 0 :(得分:1)
在public void addCard(Card c)
中,c
是parameter。
这意味着在函数addCard
的主体内,您可以引用通过名称c
传递给函数的值。您调用函数addCard
并使用某个值(例如theDeck[0]
),该值将在addCard()
中使用。