What does IndexOutofRangeException mean?

前端 未结 3 1599
梦谈多话
梦谈多话 2020-12-22 12:24

It says that in my array that I have gone over the index. My program is a Number Guessing game played by 5 players (5 indexes). I have used arrays to create the object and p

相关标签:
3条回答
  • 2020-12-22 12:34

    It means that you are trying to access an item in an array with an index higher than the limit of the array.

    0 讨论(0)
  • 2020-12-22 12:39

    It means that you are trying to access an index bigger than the array. In the line:

    while(playedThisRound[index] == false)
    

    You don't check the boundaries before using the index, and your crash is probably there.

    0 讨论(0)
  • 2020-12-22 12:47

    i is within the bounds of nPlayer length not 0-4.

    public Player[] getWinner(Player[] nPlayers)
      {
          int maxScore = 0; //edit
          Player[] winningPlayers;
          winningPlayers = new Player[5];
          for (int i = 0; i < nPlayers.Length; i++)
          {
              if (nPlayers[i].wins >= maxScore)
              {
                  winningPlayers[i].wins = nPlayers[i].getWins();
                  winningPlayers[i].name = nPlayers[i].getName();
              }
          }
          return winningPlayers;
      }
    
    0 讨论(0)
提交回复
热议问题