Java Array Index Out of Bounds somehow?

爷,独闯天下 提交于 2019-11-29 18:36:34

The problem is with your loop

while (something.equals("yes"))

There's nothing that sets something to any other value, so this loop just goes around endlessly, until all the players have more than 52 cards. Once someone has more than 52 cards, adding a new card causes the exception.

I think you need to remove this while. The code inside it should only be run once.

Your draw method is broken.

// get the first non-null Card from the cards "c".
public static Card draw(Card[] c) {
  if (c != null) {
    for (int i = 0; i < c.length; i++) {
      if (c[i] != null) {
        try {
          return c[i];
        } finally {
          // now remove element i from the `c` array.
          c[i] = null;
        }
      }
    }
  }
  return null;
}

I think the order of your code is incorrect (hard to tell with this code)

for (int i = 1; i < 8; i++)
{
 one.addCard(Card.draw(deck));
 deck = Card.getDeck();
 two.addCard(Card.draw(deck));
 deck = Card.getDeck();
}

maybe should be

for (int i = 1; i < 8; i++)
{
 deck = Card.getDeck();
 one.addCard(Card.draw(deck));
 deck = Card.getDeck();
 two.addCard(Card.draw(deck));
}

Update

Also

public void addCard(Card c) {
    hand[handsize] = c;
    handsize++;
}

handsize is never incremented - it is always 0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!