【发布时间】:2015-11-08 19:58:45
【问题描述】:
我正在制作一个纸牌游戏,它运行良好,但我无法在不遇到 IndexOutOfBoundsException 错误的情况下到达牌组底部。我有一个 1 到 52 之间的随机数字数组,我用它来选择从我拥有的卡片数组列表中调用的索引。由于卡片组是一个 ArrayList,我可以在每次循环后轻松移除每张卡片。但是我无法从随机数数组中删除数字,因此随着卡片列表变小,调用索引过高的机会就越大。想不出办法。我尝试使用 if 语句将随机数的值更改为小于 arrayList 的最大长度的值,但仍然出现错误。
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Welcome to War! Press enter to begin."); //User prompt
Scanner scanner = new Scanner(System.in); //Scanner init
String[] suit = {" of Diamonds", " of Spades", " of Hearts", " of Clubs"};//Array for suits
String[] faces = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};//Array for faces
String[] deck = new String[52]; //Deck size setup
boolean deckComplete = false;//Boolean for deck being finished
int[] random = new int[52];//Array for all numbers between 0 and the number of cards in the deck multiplied by number of decks
ArrayList<Integer> random1 = new ArrayList<Integer>();
int playerOneTotal = 0;
int playerTwoTotal = 0;
for (int x = 0; x<random.length; x++) {
random[x] = x;
}//Content setup for random array
Random rndNum = new Random();//Random init
int i = 0;
for (i = 0; i < deck.length; i++) {
deck[i] = faces[i % 13] + suit[i % 4];
}//Deck of cards setup
ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(deck));//Changing Array with card contents into an ArrayList
while (deckComplete == false) { //While loop for dealing cards
for (int j = deck.length; j >= 1; j--) {
// System.out.println("Hit enter to be dealt a card!");
String readString = scanner.nextLine(); //Set variable "readString" to user input
int randomNumber = rndNum.nextInt(j);
int randomNumberTwo = rndNum.nextInt(j);
if (readString.equals("")) { //If user input equals "enter"...
if (randomNumber >= arrayList.size()) {
randomNumber = arrayList.size() - 1;
}
if (randomNumberTwo == arrayList.size()) {
randomNumberTwo = arrayList.size() - 1;
}
int playerOne = random[randomNumber];
int playerTwo = random[randomNumberTwo];
int playerOneScore = 0;
int playerTwoScore = 0;
System.out.println("Player One Draws a: " + arrayList.get(random[randomNumber])); //Print out card
arrayList.remove(random[randomNumber]); //Remove card from deck
System.out.println("Player Two Draws a: " + arrayList.get(random[randomNumberTwo])); //Print out card
arrayList.remove(random[randomNumberTwo]); //Remove card from deck
playerOneTotal = playerOneTotal + playerOneScore;
playerTwoTotal = playerTwoTotal + playerTwoScore;
if (playerOneTotal > playerTwoTotal) {
System.out.println("Player One Wins this round! Current Score - Player One: " + playerOneTotal + " Player Two: " + playerTwoTotal);
}else if (playerOneTotal < playerTwoTotal) {
System.out.println("Player Two Wins this round! Current Score - Player One: " + playerOneTotal + " Player Two: " + playerTwoTotal);
}else if (playerOneTotal == playerTwoTotal) {
}
if (j == 1) { //If program gets to last card...
deckComplete = true; //Set desk complete to true
System.out.println(arrayList.get(random[randomNumber])); //Print last card
arrayList.remove(random[randomNumber]); //Remove last card
System.out.println("You are out of cards!"); //Print "You are out of cards"
break; //Stop loop
}
}
}
除了尝试(如下)之外,还有什么方法可以让我得到正确的结果吗?
if (randomNumber >= arrayList.size()) {
randomNumber = arrayList.size() - 1;
}
if (randomNumberTwo == arrayList.size()) {
randomNumberTwo = arrayList.size() - 1;
}
编辑:这是确切的错误消息。
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 33, Size: 23
at java.util.ArrayList.rangeCheck(ArrayList.java:635)
at java.util.ArrayList.get(ArrayList.java:411)
at Game.main(Game.java:72)
【问题讨论】:
-
请发布您得到的异常的完整堆栈跟踪。
-
@LittleSanti 对不起。已更新。
-
也许将 nexInt 参数设置为
j的最小值并且列表大小就足够了?您可以使用 Math.min -
@sergioFC 感谢您的回复。你能澄清一点吗?我不确定我是否理解。
-
我错了,我想我没有遇到问题
标签: java arrays loops arraylist