[英]Why am I getting an IndexOutOfRangeException in this code?
我基本上需要將任意數量的內容數組存儲為整數,但是之后我必須將其全部回顯。
我收到indexoutofrange錯誤。
for (int index = 0; index < userArray; index++, userArray--)
{
Console.WriteLine("Number " + userArray + " Value is:");
userArrayinputed[userArray] = int.Parse(Console.ReadLine());
}
所有代碼:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("What is the Size of the Array?");
string inputArray = Console.ReadLine();
int userArray = Convert.ToInt32(inputArray);
int[] userArrayinputed = new int[userArray];
for (int index = 0; index < userArray; index++, userArray--)
{
Console.WriteLine("Number " + userArray + " Value is:");
userArrayinputed[userArray] = int.Parse(Console.ReadLine());
}
for (int index = userArray; index > 0; index--, userArray--)
{
Console.WriteLine(userArrayinputed);
Console.ReadLine();
}
正確的代碼:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("What is the Size of the Array?");
string inputArray = Console.ReadLine();
int userArray = Convert.ToInt32(inputArray);
int maxArray = userArray;
int[] userArrayinputed = new int[userArray];
for (int index = 0; index < userArray; index++)
{
Console.WriteLine("Number " + index + " Value is:");
userArrayinputed[index] = int.Parse(Console.ReadLine());
}
for (int index = 0; index < userArray; index++)
{
Console.WriteLine(userArrayinputed[index]);
Console.ReadLine();
}
因此,用於索引的數組是基於零的,這意味着如果您想要一個10
的數組,則索引器將為0-9
。
因此,當您向上移動數組( 0-9
)時,您希望在向下移動數組( 9-0
)時for
循環的頂部為<
(小於數組長度),您希望下限為>= 0
(小於或等於數組的底部),否則您將開始嘗試以10
(數組長度)進行訪問並獲得OutOfRangeException
。
例如:
for (int i = 0; i < myArray.Length -1; i++)
{ ... }
和
for (int i = myArray.Length - 1; i >= 0; i--)
{ ... }
當您在for
循環中顯示索引時,您將要顯示索引而不是數組長度。
還要注意一點-您在兩個單獨的for循環中扣除了userArray
變量的值,當它離開循環時不會重置它,因此在方法結束時, userArray
變量應位於-(2 * userArray),而不是我認為要使用的是索引/數組長度。
所以看起來像這樣
static void Main(string[] args)
{
Console.WriteLine("What is the Size of the Array?");
string inputArray = Console.ReadLine();
int userArray = Convert.ToInt32(inputArray);
int[] userArrayinputed = new int[userArray];
for (int index = 0; index < userArray; index++)
{
Console.WriteLine("Number " + index + " Value is:");
//note you will get an error here if you try and parse something that isn't an interger
userArrayinputed[index] = int.Parse(Console.ReadLine());
}
for (int index = userArray -1; index >= 0; index--)
{
Console.WriteLine(userArrayinputed[index]);
}
Console.ReadLine();
}
更換
userArrayinputed[userArray]
同
userArrayinputed[index]
並從for
循環中刪除userArray--
。 使用索引來顯示當前數字:
for (int index = 0; index < userArray; index++)
{
Console.WriteLine("Number " + index + " Value is:");
...
數組的索引從0到(userArray -1)
string inputArray = Console.ReadLine();
int userArray = Convert.ToInt32(inputArray);
int[] userArrayinputed = new int[userArray];
for (int index = 0; index < userArray; index++)
{
Console.WriteLine("Number " + index+ " Value is:");
userArrayinputed[index] = int.Parse(Console.ReadLine());
}
for (int index = 0; index < userArray; index++)
{
Console.WriteLine(userArrayinputed[index]);
Console.ReadLine();
}
您的for()
循環首先使用userArray == userArrayinputed.Length進行評估,因此userArrayinputed[userArray]
嘗試訪問導致IndexOutOfRangeException
的數組的第(N + 1)個元素。
數組索引從0開始,還記得嗎? 所以說3個數組
int[] arr = new int[3]
該數組具有項arr[0]
, arr[1]
和arr[2]
但沒有arr[3]
,它是第4個項。
只需使用userArrayinputed[index]
並刪除for()
語句中的userArray--
部分。 如果您仍然想以相反的順序輸入數字,則可以從userArray-1倒數:
for (int index = userArray - 1; index >= 0; index--)
{
Console.WriteLine("Number " + index + " Value is:");
userArrayinputed[index] = int.Parse(Console.ReadLine());
}
要顯示數字,您應該WriteLine數組元素,而不是數組:
for (int index = userArray - 1; index >= 0; index--)
{
Console.WriteLine(userArrayinputed[index]);
Console.ReadLine();
}
最初,由於userarray是數組的長度,所以userArrayinputed [userArray]訪問最后一個索引為userarray-1。
解決方法是
for (int index = 0; index < userArray; index++, userArray--)
{
Console.WriteLine("Number " + userArray + " Value is:");
userArrayinputed[userArray-1] = int.Parse(Console.ReadLine());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.