这是一个为卡片(花色和等级)创建对象的简单程序。 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);
}
答案 0 :(得分:0)
因为在“suits”中只有四个值。索引:0、1、2、3。