目前,我正在完成这项任务,目前我已经完成了大部分工作。
public String getRankAsString() {
String[] ranks = "2", "3", "4", "5", "6",
"7", "8", "9", "10", "J", "Q", "K", "A"
return ranks[rank];
}
和西装
public String getSuitAsString() {
String[] suits = { "Clubs", "Diamonds",
"Hearts", "Spades" };
return suits[suit];
}
我应该在实验室软件包中创建2个类,一个称为LabApp(将在其中进行测试),而DiscardPile是Stack的子类,将包含方法“ discardTopCard”,称为removeTopCards的方法,该方法接受一个引用称为Card的Card对象。此方法将允许用户请求从丢弃堆中取出所有包括CardCard的卡片,并以数组形式返回,因此该方法的返回类型为Card []。这就是我的removeTopCard atm。
public Card[] removeTopCard(Card otherCard) throws StackEmptyException {
Card[] theCard = new Card[size()];
int i = 0;
boolean check = false;
if(isEmpty()){ //in the case that there's nothing there
throw new StackEmptyException("Stack is Empty");
}
while(!isEmpty()){ //If it's not empty, pop the top card (unlimited loop that'll need to be broken by a break;)
Card top = (Card) pop(); //Casts Card to pop, which should remove it
theCard[i] = top; //removed and returned in an array
i++;
if(top.equals(otherCard)){ //checks if the card's rank and suit are the same and stops the loop if true
check = true;
break; //it ends the loop if they're equivalent, all of those cards were set to top
}
}
if(!check){ //if it can't find anything
throw new StackEmptyException("Card not found");
}
Card[] topCards = new Card[i];
for(int j = 0;j < i; j++){
topCards[j] = theCard[j]; //set the discarded value array to theCard
}
return topCards; //should return the popped numbers
}
我认为,这可能很好。
在LabApp(一种测试方法)中,我目前拥有工作正常的throwsPile.push方法
discardPile.push(new Card(41));
for(int i = 0; i < discardPile.getSize(); i++) {
Card var = discardPile.pop(); //pops the cards that are above
System.out.println(var.getRankAsString() + " of " + var.getSuitAsString());
}
}
catch (StackEmptyException SEE) {
System.out.println("StackEmptyException: " + SEE.getMessage());
}
catch (StackFullException SFE) {
System.out.println("StackFullException: " + SFE.getMessage());
}
}
}
它正在打印所需的输出,没问题
我还应该编写调用removeTopCards方法,并通过将引用传递给整数值为20的卡来测试该方法。
我知道我是这样写的
discardPile.removeTopCard(new Card(20));
但是我不知道如何通过for循环打印它。
任何帮助将不胜感激。