线程 [main](挂起(未捕获异常 ArrayIndexOutOfBoundsException))

时间:2021-03-20 04:55:53

标签: java

这是一个为卡片(花色和等级)创建对象的简单程序。 Eclipse 调试器将错误指向第 16 行(String s =ranks[this.rank] + " of " +suits[this.suit];)。

public class Card 
{
    private int rank;
    private int suit;

    public Card(int rank, int suit) 
    {
        this.rank = rank;
        this.suit = suit;
    }
    public String toString() {
        String[] ranks = {null, "Ace", "2", "3", "4", "5", "6",
                   "7", "8", "9", "10", "Jack", "Queen", "King"};
        String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"};
        String s = ranks[this.rank] + " of " + suits[this.suit];
        return s;
    }
    public static void main(String args[])
    {
        Card a=new Card(1,11);
        System.out.println(a);
    }

1 个答案:

答案 0 :(得分:0)

因为在“suits”中只有四个值。索引:0、1、2、3。

相关问题