我正在使用我不完全理解的东西,澄清一下?

I'm using something i don't entirely understand, clarification?

我的 Card class 我一直在使用 throw new IllegalException ,它导致程序编译并且大部分工作正常,但我不知道为什么?

我在我的大部分 switch 语句中都返回了一个字符串,但我将它们声明为一个 int?

结果如何?

如果我想构造一张无效的卡片,比如花色 5 的卡片,它不存在,一个简单的 "This is an invalid suit" 打印行会很好,但 IllegalException 会导致 运行-当我尝试执行主要方法时出现错误。

Class卡片:

public class Card
{

    private final int CLUBS = 0;
    private final int DIAMONDS = 1;
    private final int HEARTS = 2;
    private final int SPADES = 3;

    private int points = 0;

    private int RANK;
    private int SUIT;



    /**
     * Constructor for objects of class Card
     */
    public Card(int _rank, int _suit)
    {
        this.RANK = _rank;
        this.SUIT = _suit;
    }


    private String translateSuit(int _suit)
    {

        switch(_suit)
        {
            case 0:
                return "Clubs";
            case 1:
                return "Spades";
            case 2:
                return "Hearts";
            case 3:
                return "Diamonds";
        }
        throw new IllegalArgumentException("Invalid suit: " + _suit);
    }

    private String translateRank(int _rank)
    {
        switch(_rank)
        {

            case 0:
                return "Ace";
            case 1:
                return "Two";
            case 2:
                return "Three";
            case 3:
                return "Four";
            case 4:
                return "Five";
            case 5: 
                return "Six";
            case 6:
                return "Seven";
            case 7:
                return "Eight";
            case 8:
                return "Nine";
            case 9:
                return "Ten";
            case 10:
                return "Jack";
            case 11:
                return "Queen";
            case 12:
                return "King";

        }
        throw new IllegalArgumentException("Invalid rank: " + _rank);
    }

    public void setRank(int _rank)
    {
        this.RANK = _rank;
    }

    public int getRank()
    {
        return this.RANK;
    }

    public void setSuit(int _suit)
    {
        this.SUIT = _suit;
    }

    public int getSuit()
    {
        return this.SUIT;
    }

    public String toString()
    {
        return this.translateRank(RANK) + " of " + this.translateSuit(SUIT) + " -- points: " + this.points;
    }
}

还有我的主要方法,就是测试构造函数的确认。我被要求创建 6 张卡片,2 张有效卡片,3 张无效卡片(1 套无效套装,1 套无效等级,以及 1 张两者),以及 1 张随机卡片(仍在努力解决这个问题)

public static void main(String [ ] args)
    {


        int testNum = 1;
        Card twoOfClubs = new Card(1, 0);
        Card aceOfHearts = new Card(0, 2);
        Card invalid1 = new Card(12, 5);
        Card invalid2 = new Card(15, 2);


        System.out.println(testNum + ": " +
            (twoOfClubs.toString().equals("Two of Clubs -- points: 0") 
                                            ? "Pass" : "Fail"));
        ++testNum;                                    
        System.out.println(testNum + ": " +
            (aceOfHearts.toString().equals("Ace of Hearts -- points: 0") 
                                            ? "Pass" : "Fail"));





        System.out.println(twoOfClubs);
        System.out.println(aceOfHearts);
        System.out.println(invalid1);
        System.out.println(invalid2);

    }

i'm using the throw new IllegalException and it results in the program compiling and most of it working, but i dont know why?

基本上来说,Exception是用来处理运行时发生的问题,而不是编译时。 这就是您的代码编译的原因。 您提到的行是有效的代码行,就像任何其他行一样。

Im returning a String throughout most of my switch statements but i declared them an int?

您确定这是您的代码吗? 你的一些方法 return 一个 String 类型的值,是的。 两种类型,您在方法签名中指定的类型和您 return 匹配的值的类型:String。 提示:如果不是这种情况,您的代码将无法编译。

你所指的"them"可能是你大部分方法的参数类型,确实是int。 但是,这与 return 值的类型完全无关。

and In the case i want to construct an invalid card, like one with suit 5, which doesn't exist, a simple "This is an invalid suit" print line would be nice but the IllegalException causes a run-time error when i try to excecute the main method.

当然,打印出上述行就可以完成工作。 但是在程序中使用异常是一种更加通用的处理方式 "problems"。

一个简单的 println 是一行,您可以轻松地将其插入您的卡片 class。 但是你应该意识到,处理使用过程中出现的问题并不是卡片 class 的关注点。 如果您不想打印该行,而是打开一个带有描述错误消息的弹出窗口 window 怎么办? 将创建 window 的所有代码添加到 Card class 中是一个坏主意,因为它与实际的 Card 无关。

您可能会想说“那又怎样,我只是创建特殊的 return 值,例如“”(空字符串)或 "error" 来告诉调用者出现问题的方法。”,这将允许您将处理可能错误的代码迁移到 Card class 之外。 然而,这只不过是异常所做的一个糟糕版本。

长话短说: 将对抛出异常的方法的调用包装到 try/catch(/finally) 块中,以检查执行此方法期间是否存在任何问题。