java线程[main](挂起(未捕获异常ArrayIndexOutOfBoundsException))
这是一个简单的程序来创建一个对象卡(西装和排名)。Eclipse调试器将错误指向第16行(字符串s=ranks[this.rank]+“+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,1,2,3