嘿伙计们在为我的 Java2 类编写程序时遇到了另一个问题。
我创建了一个 Deck() 类,在其中定义了以下方法:
[ isEmpty()、shuffleDeck()、dealCards()、cut()、putCardsInDeck(),最后是 toString()]
我已经为所有这些,到目前为止,它们都运行良好,这要感谢一些在这个网站上帮助过我的用户。
无论如何,我正在尝试创建一个应用程序类,在该类中测试我在甲板上的几种方法。我知道我可以很容易地调用方法打印出来并硬编码大多数其他的东西来测试这些方法。但我想超越这一点。
我正在创建一个允许用户通过按下按钮来测试这些方法的 GUI,在这里我遇到了 dealCards() 和 putCardsInDeck() 方法的小问题。
这是他们应该做什么的简要描述:
dealCards():这个方法将移除并返回存储在 theDeck 开头的卡片。
public String dealCard()
{
Node current;
Card cTemp = null;
if(isEmpty())
throw new DeckException("theDeck","empty");
else
{
current = theDeck;
cTemp = current.getItem();
theDeck = theDeck.getNext();
numCards--;
}
return cTemp.toString();
}
putCardsInDeck():这将接受对 Card 数组的引用。阵列中的牌将被移除并存储在牌组中,阵列为空。从阵列中取出的牌应存放在牌组的底部。如果数组中的卡片数量会导致牌组大小大于 52,则应抛出 DeckException 并应包含适当的消息。
public void putCardsInDeck( Card [] cards ) throws DeckException
{
Card[] theCards = cards;
Node current = theDeck;
Node lastCard = theDeck;
Node temp;
int totalCards = theCards.length + numCards;
if (totalCards > 52)
throw new DeckException("Deck", "full");
else
{
// Using current to go to the end of the deck
for(int j=0; j< ( numCards - 1 ); j++)
current = current.getNext();
// Storing the where current landed in lastCard
lastCard = current;
// Using a for loop to go thru the array and put the cards after the lastCard
for (int i=0; i < theCards.length; i++)
{
temp = new Node(new Card(theCards[i].getValue()), null);
lastCard.setNext(temp);
lastCard = lastCard.getNext();
theCards[i] = null;
numCards++;
}
}
}
这是我的 DeckException 类的代码
@SuppressWarnings("serial")
public class DeckException extends RuntimeException {
public DeckException (String collection, String status)
{
super ("The " + collection + " is "+ status + ".");
}
}
GUI 代码
好的,现在到给我带来问题的部分。在下面的代码中,我使用了一个在甲板上执行方法的 GUI(我还没有完成: putCardsInDeck (还没有完成,因为我认为我会遇到与使用 DealCards 方法相同的问题)或顺便说一句,新的甲板按钮(你会看到)。
public static void main (String [] args)
{
// Creating a new Deck
Deck theDeck = new Deck();
String listOfCards = "";
int numCardsDealt;
String[] choices = {"Display Deck", "Shuffle Deck", "Cut Deck", "Deal Deck","PutCardsInDeck", "New Deck", "Exit"};
int choice = JOptionPane.showOptionDialog(null,
"Enter your choice....",
"Main Menu",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
choices,
choices[0]);
while(choice !=6 && choice !=-1)
{
//try{
switch(choice)
{
// Just calls theDeck.toString() and displays it on a scrollpane
case 0:
JTextArea text = new JTextArea(20,20);
JScrollPane scroll = new JScrollPane(text);
text.setText(theDeck.toString());
JOptionPane.showMessageDialog(
null,
scroll,
"The Deck",
JOptionPane.DEFAULT_OPTION);
break;
// Displays the status of theDeck before the deck is shuffled and then after
case 1:
text = new JTextArea(20,20);
scroll = new JScrollPane(text);
text.setText("TheDeck before shuffleDeck method:\n" + theDeck.toString());
theDeck.shuffleDeck();
text.append("\n\nTheDeck after shuffleDeck method:\n" + theDeck.toString());
JOptionPane.showMessageDialog(
null,
scroll,
"Testing the shuffleDeck() method on theDeck.",
JOptionPane.DEFAULT_OPTION);
break;
// Displays the status of theDeck object before the cut method and after
case 2:
text = new JTextArea(20,20);
scroll = new JScrollPane(text);
text.setText("TheDeck before cut method: \n" + theDeck.toString());
theDeck.cut();
text.append("\n\nTheDeck after cut method: \n" + theDeck.toString());
JOptionPane.showMessageDialog(
null,
scroll,
"Testing out the cut() method on theDeck",
JOptionPane.DEFAULT_OPTION);
break;
// Displays the status of theDeck before cards are dealt
// Asks user to select a number of cards and deals that amount of cards
// Displays the status of theDeck after the cards have been dealt
case 3:
text = new JTextArea(20,20);
scroll = new JScrollPane(text);
text.setText("TheDeck before dealCard method: \n" + theDeck.toString());
numCardsDealt = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter the number of cards you want dealt..."));
try
{
for( int i = 0; i<numCardsDealt; i++)
listOfCards+="Card dealt " + (i+1) + ": " +theDeck.dealCard() + "\n";
text.append("\n\nList of cards dealt:\n" + listOfCards);
text.append("\n\nTheDeck after dealCard method: \n" + theDeck.toString());
JOptionPane.showMessageDialog(
null,
scroll,
"The Deck before its been cut.",
JOptionPane.DEFAULT_OPTION);
}
catch(DeckException ex)
{
JOptionPane.showMessageDialog(null, "There are not enough cards in \nthe deck" +
"to satisty the number\nof cards you have entered to be dealt.");// \n"Please try a different number");
}
break;
} // ends switch
choice = JOptionPane.showOptionDialog(null,
"Enter your choice....",
"Main Menu",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
choices,
choices[0]);
//} // ends try
//catch(NullPointerException ex)
//{
//System.out.println("NullPointerException");
//}
//catch(NumberFormatException ex)
//{
//System.out.println("NumberFormatException");
//}
}// ends while loop
当我的应用程序类运行并且用户单击 DealCard 按钮时,系统会要求用户输入一个数字,但假设用户没有输入我得到的数字:
NumberFormatException (NFE):线程“main”java 中的异常。 lang.NumberFormatException:对于输入字符串:“”
,当我按下取消时,我得到
NumberFormatException (NFE):线程“main”中的异常 java.lang.NumberFormatException: null
当我按下红色 X 关闭输入对话框时,我得到另一个
NumberFormatException ( NFE):线程“main”中的异常 java.lang.NumberFormatException:null
最后我的观点是:
我希望能够弥补这些情况,如果用户没有输入任何内容,弹出窗口应该会继续要求用户输入一个数字,直到他们按下红色 X 或取消。
但是当我使用 try/catch 块时,无论我是否按下红色 X,输入对话框都会不断返回,不要输入任何内容或按下取消。=/(我的另一个想法是如果用户将输入对话框留空,则生成一个随机数......但一次一步^_^)
PS您将在我的代码中看到我有另一个 try/catch 块以防案例 3那是为了捕获另一个异常,以防止用户发的牌比牌组中的牌多。(我想我必须创建另一种名为 size() 的方法来查看牌组中有多少张牌,但是 IDK ......你们怎么看)
所以是的,就是这样。先感谢您。