Java 索引超出范围异常

Java 索引超出范围异常,java,arrays,Java,Arrays,我正在尝试写一个猜谜游戏,其中我们不允许使用同一张牌两次,我们需要使用15张牌(对象)的数组。我已经创造了一切(可能不是以理想的方式)但是我不断得到错误java.land.ArrayIndexOutOfBoundException:索引15超出长度15的界限当我试图正确回答所有问题时,它无法完成一副牌。我不明白为什么。以下是相关代码: while (CountryCard.instances < 30) { System.out.println("Would you li

我正在尝试写一个猜谜游戏,其中我们不允许使用同一张牌两次,我们需要使用15张牌(对象)的数组。我已经创造了一切(可能不是以理想的方式)但是我不断得到错误java.land.ArrayIndexOutOfBoundException:索引15超出长度15的界限当我试图正确回答所有问题时,它无法完成一副牌。我不明白为什么。以下是相关代码:

while (CountryCard.instances < 30) {
    System.out.println("Would you like to guess the capital of a country?"
            + " Hit 1 for yes, or 2 for no (Case sensitive)");
    yesNo = input.nextInt();
    if (yesNo == 2) {
        return;
    }
    int randomNumber = 0 + (int) (Math.random() * ((game.length)+1 ));
    while(game[randomNumber].used==true){
        randomNumber = 0 + (int) (Math.random() * ((game.length)+1 ));
    }
    System.out.println("What is the Capital of "
            + game[randomNumber].getName() + "?");
    game[randomNumber].usedCard(1);
    guess = input.next();
    if (guess.equals(game[randomNumber].getCapital())) {
        System.out.println("Correct! :)");
    } else {
        System.out.println("Incorrect! :(");
    }
}//end of while
System.out.println("Thanks for playing :)");
while(CountryCard.instances<30){
System.out.println(“你想猜一个国家的首都吗?”
+“点击1表示是,或点击2表示否(区分大小写)”;
yesNo=input.nextInt();
如果(是否==2){
返回;
}
int randomNumber=0+(int)(Math.random()*((game.length)+1));
while(游戏[randomNumber]。使用==true){
randomNumber=0+(int)(Math.random()*((game.length)+1));
}
System.out.println(“的资本是什么?”
+游戏[randomNumber].getName()+“?”;
游戏[randomNumber]。使用卡片(1);
guess=input.next();
if(guess.equals(game[randomNumber].getCapital())){
System.out.println(“正确!:)”;
}否则{
System.out.println(“不正确的!:(”);
}
}//结束
System.out.println(“感谢播放:)”;

数组索引基于0,这意味着第15个元素的索引为14

任何数组/列表的索引都以0开头,因此长度为9的对象的最大对象索引为8

char[]a=“Apple.toCharArray();
所以a的长度是5,但a[4]='e'