是什么IndexOutofRangeException意思

是什么IndexOutofRangeException意思

本文介绍了是什么IndexOutofRangeException意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它说,在我的数组,我已经过了指数。我的计划是一个猜数字游戏由5名球员(5个指标)播放。我已经使用数组来创建对象和播放器类。
在我的节目在第二或第三轮的比赛中崩溃内我已经达到了一个树桩。我发现我的第二轮比赛中,指数并没有loop属性:循环在第一循环计数指数1到5,然后计算2至5在第二循环中,那么如果我什至第三轮循环,所有的指标都是围绕这意味着我不能在1到5洗牌。

由于每个球员得到3猜测,使用这些猜测3和你的游戏了。我已我的播放器创建对象的阵列,创建的临时数组比previous较小和引用的,为实现当前阵列。

我看了看我的引用在code,发现高达code,因为我能解决,我无法找到导致我System.IndexOutOfRangeException的bug。它是由我的猜谜游戏类引起的。

下面是我的GuessingGame类:

 使用系统; //这里需要仅此using语句。命名空间GuessingGame
{类GuessingGame
{
  #区域实例属性
  私人const int的GUESSES_ALLOWED = 3;
  私人const int的NUMBER_OF_PLAYERS_TO_START = 5;
  私人const int的MIN_VALUE = 1;
  私人const int的MAX_VALUE = 15;
  私人播放器[]的玩家;
  私人随机randomSource;
  #endregion  公共GuessingGame()
  {
     Console.WriteLine(启动GuessingGame的构造函数);
     玩家=新播放器[NUMBER_OF_PLAYERS_TO_START]
     randomSource =新的随机();     字符串playerName =;
     对于(INT指数= 0;指数 - LT; players.Length;指数++)
     {
        Console.Write(什么是玩家#姓名
              +(索引+1)+\\ t?);
        playerName =到Console.ReadLine();
        玩家[指数] =新播放器(playerName,randomSource);
        Console.Write(\\ n);
     }
     Console.WriteLine(结束GuessingGame构造);
  }  公共GuessingGame(字符串[] playerNames)
  {     Console.WriteLine(启动GuessingGame的构造函数);
     玩家=新播放器[playerNames.Length]
     randomSource =新的随机();
     对于(INT指数= 0;指数 - LT; playerNames.Length;指数++)
     {
        玩家[指数] =新播放器(playerNames [指数],randomSource);
     }
  }  公共无效琐事()
  {
     INT numberOfPlayersWhoHavePlayedThisRound = 0;
     INT索引= 0;     布尔[] playedThisRound = NULL;
     字符串playerGuessEntry =;
     INT playerGuessValue = -1;
     播放器[] = tempArray新的播放器[players.Length - 1];
     布尔roundOver = FALSE;     Console.WriteLine(
           启动琐事 - preSS任意键继续);
     //Console.Read()     而(roundOver == FALSE)//这是正确的状态?
     {         playedThisRound =新布尔[players.Length]
         而(playedThisRound [指数] == FALSE)
         {
             做
             {
                 Console.Write(玩家[指数] .getName()
                       +,输入之间的数字
                       + MIN_VALUE.ToString()
                       +和+ MAX_VALUE.ToString()
                       +包容\\ t的);
                 playerGuessEntry =到Console.ReadLine();
                 Console.Write(\\ n);
             }
             而(!int.TryParse(playerGuessEntry,
                      出playerGuessValue)
                    || playerGuessValue< MIN_VALUE
                    || playerGuessValue> MAX_VALUE);
             如果(playerGuessValue< MIN_VALUE || playerGuessValue> MAX_VALUE)
             {
                 Console.Write(无效guess-重试);
             }
             其他
             {                 Console.WriteLine(您输入
                       + playerGuessValue.ToString());                 玩家[指数] .makeAGuess(playerGuessValue);
                 playedThisRound [指数] =真;
                如果(指数== players.Length)
                {
                    Console.WriteLine(回合结束);
                    索引= 0; //编辑?
                    numberOfPlayersWhoHavePlayedThisRound = 0;
                }             }
             如果(玩家[指数] .getGuessesUsed()== 3)
             {//创建一个临时数组
                 Console.WriteLine(猜测刷爆);
                 tempArray =玩家[指数] .deletePlayerFromArray(播放器,索引);
                 玩家= tempArray; //引用
                 布尔[] = tempBooleanArray新布尔[playedThisRound.Length - 1]; //减少发挥这一轮数组的大小
                 Console.WriteLine(Playedthisround长:+ playedThisRound.Length +\\ n该指数:+ index.ToString());
                 tempBooleanArray =玩家[指数] .deletePlayerBool(playedThisRound,指数);
                 playedThisRound = tempBooleanArray;
                 Console.WriteLine(新玩家数组大小+ players.Length);
                 Console.WriteLine(新布尔数组大小+ playedThisRound.Length);
             }
             如果(指数== players.Length - 1)
             {
                 索引= 0;
                 numberOfPlayersWhoHavePlayedThisRound = 0;
             }
             如果(players.Length == 1)
             {
                 roundOver = TRUE;
             }
             指数++;
             numberOfPlayersWhoHavePlayedThisRound ++;
         }
            Console.WriteLine(WINNER:+玩家[指数] .getName()+
                \\ nWins:+玩家[指数] .getWins()+\\ nArray尺寸:+ players.Length.ToString());     } //而结束     Console.WriteLine(结束琐事 -
           +preSS任意键继续);
     Console.Read();
  }
       公共BOOL playersAlreadyPlayed(布尔[] thePlayer)
      {
          布尔havePlayed = FALSE;
          对(INT起着= 0;播放&下; thePlayer.Length;起着++)
          {
              如果(thePlayer [播放] == FALSE)
              {
                  havePlayed = FALSE;
              }
              其他
              {
                  havePlayed = TRUE;
              }
          }
          返回havePlayed;
      }  静态无效的主要(字串[] args)
  {
     GuessingGame newGame =新GuessingGame();
     newGame.playGame();
  }
 }

}

和这里是玩家级

 使用系统;命名空间GuessingGame
{   类播放器
   {      私人字符串名称;
      私人诠释winningNumber;
      私人诠释guessesUsed;
      私人诠释胜;
      私人随机myWinningNumberSource;      公众播放器(串了newName,随机随机)
      {
         名称=了newName;
         guessesUsed = 0;
         胜= 0;
         myWinningNumberSource =随机;
         winningNumber = myWinningNumberSource.Next(1,16);
      }
      公共BOOL makeAGuess(INT guessValue)
      {
          布尔isWinner = FALSE; //编辑
         如果(guessValue == winningNumber)
         {
            胜++;            Console.WriteLine(Congradulations,你已经猜到了正确的号码\\ n!);
            Console.WriteLine(你有一个总的+胜+胜利!);
            Console.WriteLine(你+(3 - guessesUsed)+!猜测离开\\ n);
            winningNumber = myWinningNumberSource.Next(1,16);
            isWinner = TRUE; //编辑         }
         其他
         {
            guessesUsed ++;            Console.WriteLine(!噢,不你已经猜到incorretly!);
            Console.WriteLine(你曾用+ guessesUsed +,并有+(3 - guessesUsed)+左猜测!);
            Console.WriteLine(提示:你应该已经猜到+ winningNumber);
            isWinner = FALSE;
            如果(guessesUsed→3)
            {
                Console.WriteLine(对不起,你已经失去了,游戏结束);            }         }
         返回isWinner;
      }      公众诠释getGuessesUsed()
      {
         返回guessesUsed;
      }      公共字符串的getName()
      {
         返回名称;
      }
      公众诠释getWins()
      {
          返回胜;
      }
      公众播放器[] getWinner(播放器[] nPlayers)
      {
          INT maxScore = 0; //编辑
          播放器[] winningPlayers;
          winningPlayers =新播放器[5];
          的for(int i = 0; I< nPlayers.Length;我++)
          {
              如果(nPlayers [I] .wins> = maxScore)
              {
                  winningPlayers [I] .wins = nPlayers [I] .getWins();
                  winningPlayers [我]。名称= nPlayers [I] .getName();
              }
          }
          返回winningPlayers;
      }
      公共BOOL [] deletePlayerBool(布尔[] playedThisRound,INT removeIndex)//编辑
      {
          布尔[] newArray =新布尔[playedThisRound.Length - 1];
          INT tempIndex = 0;
          的for(int i = 0; I< playedThisRound.Length;我++)
          {
              如果(我!= removeIndex)
              {
                  newArray [tempIndex ++] = playedThisRound [I]
              }
          }
          返回newArray;
      }
      公众播放器[] deletePlayerFromArray(播放器[] nPlayers,INT removeIndex)
      {
          播放器[] newArray =新播放器[nPlayers.Length - 1];
          INT tempIndex = 0;
          的for(int i = 0; I< nPlayers.Length;我++)
          {
              如果(我!= removeIndex)
              {
                  newArray [tempIndex ++] = nPlayers [I]
              }
          }
          返回newArray;
      }   }
}


解决方案

我是nPlayer长度范围内没有0-4。

 公开播放器[] getWinner(播放器[] nPlayers)
  {
      INT maxScore = 0; //编辑
      播放器[] winningPlayers;
      winningPlayers =新播放器[5];
      的for(int i = 0; I< nPlayers.Length;我++)
      {
          如果(nPlayers [I] .wins> = maxScore)
          {
              winningPlayers [I] .wins = nPlayers [I] .getWins();
              winningPlayers [我]。名称= nPlayers [I] .getName();
          }
      }
      返回winningPlayers;
  }

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 player classes.I have reached a stump where my program crashes within the second or third round of the game. I noticed that during my second round, the index did not loop property: the loop counts the index 1 to 5 in the first loop, then counts 2 to 5 in the second loop, then if I even get to the 3rd round of the loop, all the indexes are shuffled around meaning I can't go from 1 to 5.

As each player gets 3 guesses, use those 3 guesses and your out of the game. I have taken the array of object I created for the player, created a temporary array smaller than the previous and referenced that to achieve the current array.

I looked over my references in the code and found as much code as I could fix, I cannot find the bug that is causing my System.IndexOutOfRangeException. It is being caused by my guessing game class.

Here is my GuessingGame Class:

using System;  // only this using statement is needed here.

namespace GuessingGame
{

class GuessingGame
{
  #region instance attributes
  private const int GUESSES_ALLOWED = 3;
  private const int NUMBER_OF_PLAYERS_TO_START = 5;
  private const int MIN_VALUE = 1;
  private const int MAX_VALUE = 15;
  private Player[] players;
  private Random randomSource;
  #endregion

  public GuessingGame()
  {
     Console.WriteLine("Starting Constructor of GuessingGame");
     players = new Player[NUMBER_OF_PLAYERS_TO_START];
     randomSource = new Random();

     string playerName = "";
     for (int index = 0; index < players.Length; index++)
     {
        Console.Write("What is the name for player #"
              + (index +1) + "?\t");
        playerName = Console.ReadLine();
        players[index] = new Player(playerName, randomSource);
        Console.Write("\n");
     }
     Console.WriteLine("Ending GuessingGame Constructor");
  }

  public GuessingGame(string [] playerNames)
  {

     Console.WriteLine("Starting Constructor of GuessingGame");
     players = new Player[playerNames.Length];
     randomSource = new Random();
     for (int index = 0; index < playerNames.Length; index++)
     {
        players[index] = new Player(playerNames[index], randomSource);
     }
  }

  public void playGame()
  {
     int numberOfPlayersWhoHavePlayedThisRound = 0;
     int index = 0;

     bool[] playedThisRound = null;
     string playerGuessEntry = "";
     int playerGuessValue = -1;
     Player[] tempArray = new Player[players.Length - 1];
     bool roundOver = false;

     Console.WriteLine(
           "Starting playGame - press any key to continue");
     //Console.Read()

     while (roundOver == false) // Is this the right condition?
     {

         playedThisRound = new bool[players.Length];


         while (playedThisRound[index] == false)
         {
             do
             {
                 Console.Write(players[index].getName()
                       + ", Enter a number between "
                       + MIN_VALUE.ToString()
                       + " and " + MAX_VALUE.ToString()
                       + " inclusive\t");
                 playerGuessEntry = Console.ReadLine();
                 Console.Write("\n");
             }
             while (!int.TryParse(playerGuessEntry,
                      out playerGuessValue)
                    || playerGuessValue < MIN_VALUE
                    || playerGuessValue > MAX_VALUE);
             if(playerGuessValue < MIN_VALUE || playerGuessValue > MAX_VALUE)
             {
                 Console.Write("Invalid guess- try again");
             }
             else
             {

                 Console.WriteLine("You entered "
                       + playerGuessValue.ToString());

                 players[index].makeAGuess(playerGuessValue);
                 playedThisRound[index] = true;
                if (index == players.Length)
                {
                    Console.WriteLine("End of Round");
                    index = 0; //edit?
                    numberOfPlayersWhoHavePlayedThisRound = 0;
                }

             }
             if (players[index].getGuessesUsed() == 3)
             {//creating a temp array
                 Console.WriteLine("Guesses MAXED");
                 tempArray = players[index].deletePlayerFromArray(players, index);
                 players = tempArray; // referencing
                 bool[] tempBooleanArray = new bool[playedThisRound.Length - 1];//reducing size of played this round array
                 Console.WriteLine("Playedthisround length: " + playedThisRound.Length + " \nThe Index: " + index.ToString());
                 tempBooleanArray = players[index].deletePlayerBool(playedThisRound, index);
                 playedThisRound = tempBooleanArray;
                 Console.WriteLine("New Player Array Size: " + players.Length);
                 Console.WriteLine("New Boolean Array Size: " + playedThisRound.Length);
             }
             if (index == players.Length - 1)
             {
                 index = 0;
                 numberOfPlayersWhoHavePlayedThisRound = 0;
             }
             if (players.Length == 1)
             {
                 roundOver = true;
             }
             index++;
             numberOfPlayersWhoHavePlayedThisRound++;
         }
            Console.WriteLine("WINNER:" + players[index].getName() +
                "\nWins: " + players[index].getWins() + "\nArray Size: " + players.Length.ToString());

     }//end of while

     Console.WriteLine("Ending playGame - "
           + "press any key to continue");
     Console.Read();
  }
       public bool playersAlreadyPlayed(bool[] thePlayer)
      {
          bool havePlayed = false;
          for (int plays = 0; plays < thePlayer.Length; plays++)
          {
              if (thePlayer[plays] == false)
              {
                  havePlayed = false;
              }
              else
              {
                  havePlayed = true;
              }
          }
          return havePlayed;
      }

  static void Main(string[] args)
  {
     GuessingGame newGame = new GuessingGame();
     newGame.playGame();
  }
 }

}

And Here is the Player Class

using System;

namespace GuessingGame
{

   class Player
   {

      private String name;
      private int winningNumber;
      private int guessesUsed;
      private int wins;
      private Random myWinningNumberSource;

      public Player(string newName, Random random)
      {
         name = newName;
         guessesUsed = 0;
         wins = 0;
         myWinningNumberSource = random;
         winningNumber = myWinningNumberSource.Next(1, 16);
      }


      public bool makeAGuess(int guessValue)
      {
          bool isWinner = false;//edit
         if (guessValue == winningNumber)
         {
            wins++;

            Console.WriteLine("Congradulations, You have guessed correct number!\n");
            Console.WriteLine("You have a total of " + wins + " wins!");
            Console.WriteLine("You have " + (3 - guessesUsed) + " guesses left!\n");
            winningNumber = myWinningNumberSource.Next(1, 16);
            isWinner = true; //edit

         }
         else
         {
            guessesUsed++;

            Console.WriteLine("Oh no! You have guessed incorretly!");
            Console.WriteLine("You have used " + guessesUsed + " and have " + (3 - guessesUsed) + " guesses left!");
            Console.WriteLine("HINT: You should have guessed " + winningNumber);
            isWinner = false;


            if (guessesUsed > 3)
            {
                Console.WriteLine("Sorry you have Lost, Game Over");

            }

         }
         return isWinner;
      }

      public int getGuessesUsed()
      {
         return guessesUsed;
      }

      public string getName()
      {
         return name;
      }
      public int getWins()
      {
          return wins;
      }
      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;
      }
      public bool[] deletePlayerBool(bool[] playedThisRound, int removeIndex)//edit
      {
          bool[] newArray = new bool[playedThisRound.Length - 1];
          int tempIndex = 0;
          for (int i = 0; i < playedThisRound.Length; i++)
          {
              if (i != removeIndex)
              {
                  newArray[tempIndex++] = playedThisRound[i];
              }
          }
          return newArray;
      }
      public Player[] deletePlayerFromArray(Player[] nPlayers, int removeIndex)
      {
          Player[] newArray = new Player[nPlayers.Length - 1];
          int tempIndex = 0;
          for (int i = 0; i < nPlayers.Length; i++)
          {
              if (i != removeIndex)
              {
                  newArray[tempIndex++] = nPlayers[i];
              }
          }
          return newArray;
      }

   }
}
解决方案

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;
  }

这篇关于是什么IndexOutofRangeException意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 03:52