繁体   English   中英

如何在GUI上使用try / catch块来测试Deck类上的方法

[英]how do I use a try/catch block on a GUI to test out methods on a Deck Class

大家在为我的Java2类编写程序时遇到了另一个问题。
我创建了一个Deck()类,其中定义了以下方法:
[isEmpty(),shuffleDeck(),dealCards(),cut(),putCardsInDeck(),最后是toString())
我已经为所有代码编写了代码,到目前为止,由于一些在此站点上为我提供帮助的用户,它们可以正常工作。

无论如何,我都试图创建一个应用程序类,在其中它在平台上测试了我的几种方法。 我知道我可以轻松地调用方法将它们打印出来,并使用硬代码来测试这些方法。 但我想超越这个范围。
我正在创建一个GUI,该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):线程“主”中的异常java.lang.NumberFormatException:对于输入字符串:“”

当我按取消我得到
NumberFormatException(NFE):线程“主”中的异常java.lang.NumberFormatException:null

当我按下红色的X关闭输入对话框时,我得到另一个
NumberFormatException(NFE):线程“主”中的异常java.lang.NumberFormatException:null

最后,我的观点是:
我希望能够弥补这些情况,如果用户未输入任何内容,则弹出窗口应继续询问用户输入数字,直到他们按下红色的X或取消为止。

但是,当我使用try / catch块时,无论我按红色X,不输入任何内容还是按取消,输入对话框都会不断返回。 = /(我的另一个想法是,如果用户将inputdialogbox留为空白,则生成一个随机数,但是一次只能一次^ _ ^)

PS:您会在我的代码中看到在第3种情况下我还有另一个try / catch块,该块可以捕获另一个异常,以防止用户发牌量超过卡组中的牌数。 (我想我必须制作另一个名为size()的方法,以查看甲板上有多少张卡片,但IDK ...你们怎么看)

是的,就是这样。 先感谢您。

*因此我设法弄清楚了,到目前为止它仍然有效。

String parseThis;
 do {
     parseThis = JOptionPane.showInputDialog(null, "Please enter the number of cards you want dealt...");
    } while (parseThis == "");

 if (parseThis == null)
    break;

 numCardsDealt = Integer.parseInt(parseThis);

如果用户未插入值,这将使inputdialogbox继续返回。 一旦用户按下取消或关闭输入对话框,它将消失。

不管怎么说,多谢拉

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM