'System.IndexOutOfRangeException' 出现在 case 开关数组中
'System.IndexOutOfRangeException' occured in an case switch array
在一个地方向我扔 'System.IndexOutOfRangeException' 一两年前我第一次做这个的时候还没有。我问了我现在的教授,但他只是说 "it works there is no problem" 我们都得到了例外,所以我不知道为什么老师没有。这是代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/* Author: Austin Bigge
* This program will ask for a ten digit alphanumeric
* number and return the numeric number.
*/
namespace alphanumeric
{
class Program
{
static void Main(string[] args)
{
// Switch for alphanumeric entry
string numberString; //alphanumberic entry
char currentLetter;
string invalue;
double pnumber; //returned single numeric phone number
int i; //i = the current character space
//User prompt
Console.WriteLine("This program will ask for an alphanumberic phone ");
Console.WriteLine("number and change it into a numeric phone number.");
Console.WriteLine("! Alphanumeric characters only !");
Console.WriteLine("! No Spaces, Dashes, or Symbols !");
Console.Write("What is the 10 digit phone number? ");
invalue = Console.ReadLine();
numberString = invalue;
char[] array = numberString.ToCharArray();
for (i = 0; i < array.Length; i++) //for loop
// gets the current letter at position "i"
currentLetter = array[i];
switch (i) //case switch for possible entries
{
case '1':
pnumber = 1; Console.WriteLine(pnumber); break;
case '2':
case 'a':
case 'A':
case 'b':
case 'B':
case 'c':
case 'C':
pnumber = 2; Console.WriteLine(pnumber); break;
case '3':
case 'd':
case 'D':
case 'e':
case 'E':
case 'f':
case 'F':
pnumber = 3; Console.WriteLine(pnumber); break;
case '4':
case 'g':
case 'G':
case 'h':
case 'H':
case 'i':
case 'I':
pnumber = 4; Console.WriteLine(pnumber); break;
case '5':
case 'j':
case 'J':
case 'k':
case 'K':
case 'l':
case 'L':
pnumber = 5; Console.WriteLine(pnumber); break;
case '6':
case 'm':
case 'M':
case 'n':
case 'N':
case 'o':
case 'O':
pnumber = 6; Console.WriteLine(pnumber); break;
case '7':
case 'p':
case 'P':
case 'q':
case 'Q':
case 'r':
case 'R':
case 's':
case 'S':
pnumber = 7; Console.WriteLine(pnumber); break;
case '8':
case 't':
case 'T':
case 'u':
case 'U':
case 'v':
case 'V':
pnumber = 8; Console.WriteLine(pnumber); break;
case '9':
case 'w':
case 'W':
case 'x':
case 'X':
case 'y':
case 'Y':
case 'z':
case 'Z':
pnumber = 9; Console.WriteLine(pnumber); break;
case '0':
pnumber = 0; Console.WriteLine(pnumber); break;
case ' ': Console.WriteLine("Bad Value, do not use spaces"); break;
default: Console.WriteLine("Bad Value, use only numbers or letters."); break;
}
}
}
}
感谢任何帮助,他一直向我寻求帮助,我一直告诉他他知道的和我一样多。
p.s。调试器指向此异常的第 36 行
编辑:
是的,你是对的,现在我在第 37 行 "currentLetter" 有一个未分配的变量,我想我已经初始化了它,当我尝试初始化它时出现错误,它告诉我它已经初始化了。 .. 我一定是删除了我的工作程序。我发现了这个文件的几个空迭代。不过,我真的很感谢您的帮助,我相信我的朋友也会如此。
我想我修好了,我用 "i"
替换了 "currentLetter"
现在阵列不工作了。只返回默认情况。再次卡住。我编辑了我的进度。
编辑
我们集思广益时发现了一堆错误。完成的程序如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Switch for alphanumeric entry
string numberString; //alphanumberic entry
char currentLetter;
string invalue;
double pnumber; //returned single numeric phone number
int i; //i = the current character space
//User prompt
Console.WriteLine("This program will ask for an alphanumberic phone ");
Console.WriteLine("number and change it into a numeric phone number.");
Console.WriteLine("! Alphanumeric characters only !");
Console.WriteLine("! No Spaces, Dashes, or Symbols !");
Console.Write("What is the 10 digit phone number? ");
invalue = Console.ReadLine();
numberString = invalue;
char[] array = numberString.ToCharArray();
for (i = 0; i < 10; i++)
{
//for loop
//numberString = invalue.ToString().ToCharArray()[i]);
// gets the current letter at position "i"
currentLetter = array[i];
switch (currentLetter) //case switch for possible entries
{
case '0':
pnumber = 0; Console.Write(pnumber); break;
case '1':
pnumber = 1; Console.Write(pnumber); break;
case '2':
case 'a':
case 'A':
case 'b':
case 'B':
case 'c':
case 'C':
pnumber = 2; Console.Write(pnumber); break;
case '3':
case 'd':
case 'D':
case 'e':
case 'E':
case 'f':
case 'F':
pnumber = 3; Console.Write(pnumber); break;
case '4':
case 'g':
case 'G':
case 'h':
case 'H':
case 'i':
case 'I':
pnumber = 4; Console.Write(pnumber); break;
case '5':
case 'j':
case 'J':
case 'k':
case 'K':
case 'l':
case 'L':
pnumber = 5; Console.Write(pnumber); break;
case '6':
case 'm':
case 'M':
case 'n':
case 'N':
case 'o':
case 'O':
pnumber = 6; Console.Write(pnumber); break;
case '7':
case 'p':
case 'P':
case 'q':
case 'Q':
case 'r':
case 'R':
case 's':
case 'S':
pnumber = 7; Console.Write(pnumber); break;
case '8':
case 't':
case 'T':
case 'u':
case 'U':
case 'v':
case 'V':
pnumber = 8; Console.Write(pnumber); break;
case '9':
case 'w':
case 'W':
case 'x':
case 'X':
case 'y':
case 'Y':
case 'z':
case 'Z':
pnumber = 9; Console.Write(pnumber); break;
case ' ':
Console.WriteLine();
Console.WriteLine("Bad Value, do not use spaces");
Console.Write("What is the 10 digit phone number? ");
invalue = Console.ReadLine();
numberString = invalue;
array = numberString.ToCharArray();
i = 0;
break;
default:
Console.WriteLine();
Console.WriteLine("Bad Value, use only numbers or letters.");
Console.Write("What is the 10 digit phone number? ");
invalue = Console.ReadLine();
numberString = invalue;
array = numberString.ToCharArray();
i = 0;
break;
}
}
Console.WriteLine();
Console.WriteLine("Press any key to close this window");
Console.ReadLine();
}
}
}
感谢您让我们开始修复,我们都很感激,朋友!
for
行中的分号导致了您的问题:
for (i = 0; i < array.Length; i++) ; //for loop
for 循环后面应该跟一个块,但在这种情况下,因为它后面跟着一个分号,后面的代码不是循环的一部分,而只是执行一次的常规代码。
所以for
循环执行时是一个空体,当它完成时,i
有一个数组长度的值,当然比最大索引高一个,抛出异常。
要解决此问题,请围绕要包含在循环中的代码组创建一个块(大括号)。
这也带来了一个单独的问题 - for
循环通常会在循环本身中声明索引器 - 如果你在 for
中定义了 i
,那么它会已经超出以下几行的范围,并且会更容易看到您的错误,因为它本来是编译器错误。
for (int i = 0; i < array.Length; i++)
{
// Your code here
}
// i is no longer in scope
在一个地方向我扔 'System.IndexOutOfRangeException' 一两年前我第一次做这个的时候还没有。我问了我现在的教授,但他只是说 "it works there is no problem" 我们都得到了例外,所以我不知道为什么老师没有。这是代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/* Author: Austin Bigge
* This program will ask for a ten digit alphanumeric
* number and return the numeric number.
*/
namespace alphanumeric
{
class Program
{
static void Main(string[] args)
{
// Switch for alphanumeric entry
string numberString; //alphanumberic entry
char currentLetter;
string invalue;
double pnumber; //returned single numeric phone number
int i; //i = the current character space
//User prompt
Console.WriteLine("This program will ask for an alphanumberic phone ");
Console.WriteLine("number and change it into a numeric phone number.");
Console.WriteLine("! Alphanumeric characters only !");
Console.WriteLine("! No Spaces, Dashes, or Symbols !");
Console.Write("What is the 10 digit phone number? ");
invalue = Console.ReadLine();
numberString = invalue;
char[] array = numberString.ToCharArray();
for (i = 0; i < array.Length; i++) //for loop
// gets the current letter at position "i"
currentLetter = array[i];
switch (i) //case switch for possible entries
{
case '1':
pnumber = 1; Console.WriteLine(pnumber); break;
case '2':
case 'a':
case 'A':
case 'b':
case 'B':
case 'c':
case 'C':
pnumber = 2; Console.WriteLine(pnumber); break;
case '3':
case 'd':
case 'D':
case 'e':
case 'E':
case 'f':
case 'F':
pnumber = 3; Console.WriteLine(pnumber); break;
case '4':
case 'g':
case 'G':
case 'h':
case 'H':
case 'i':
case 'I':
pnumber = 4; Console.WriteLine(pnumber); break;
case '5':
case 'j':
case 'J':
case 'k':
case 'K':
case 'l':
case 'L':
pnumber = 5; Console.WriteLine(pnumber); break;
case '6':
case 'm':
case 'M':
case 'n':
case 'N':
case 'o':
case 'O':
pnumber = 6; Console.WriteLine(pnumber); break;
case '7':
case 'p':
case 'P':
case 'q':
case 'Q':
case 'r':
case 'R':
case 's':
case 'S':
pnumber = 7; Console.WriteLine(pnumber); break;
case '8':
case 't':
case 'T':
case 'u':
case 'U':
case 'v':
case 'V':
pnumber = 8; Console.WriteLine(pnumber); break;
case '9':
case 'w':
case 'W':
case 'x':
case 'X':
case 'y':
case 'Y':
case 'z':
case 'Z':
pnumber = 9; Console.WriteLine(pnumber); break;
case '0':
pnumber = 0; Console.WriteLine(pnumber); break;
case ' ': Console.WriteLine("Bad Value, do not use spaces"); break;
default: Console.WriteLine("Bad Value, use only numbers or letters."); break;
}
}
}
}
感谢任何帮助,他一直向我寻求帮助,我一直告诉他他知道的和我一样多。 p.s。调试器指向此异常的第 36 行
编辑:
是的,你是对的,现在我在第 37 行 "currentLetter" 有一个未分配的变量,我想我已经初始化了它,当我尝试初始化它时出现错误,它告诉我它已经初始化了。 .. 我一定是删除了我的工作程序。我发现了这个文件的几个空迭代。不过,我真的很感谢您的帮助,我相信我的朋友也会如此。
我想我修好了,我用 "i"
替换了 "currentLetter"现在阵列不工作了。只返回默认情况。再次卡住。我编辑了我的进度。
编辑
我们集思广益时发现了一堆错误。完成的程序如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Switch for alphanumeric entry
string numberString; //alphanumberic entry
char currentLetter;
string invalue;
double pnumber; //returned single numeric phone number
int i; //i = the current character space
//User prompt
Console.WriteLine("This program will ask for an alphanumberic phone ");
Console.WriteLine("number and change it into a numeric phone number.");
Console.WriteLine("! Alphanumeric characters only !");
Console.WriteLine("! No Spaces, Dashes, or Symbols !");
Console.Write("What is the 10 digit phone number? ");
invalue = Console.ReadLine();
numberString = invalue;
char[] array = numberString.ToCharArray();
for (i = 0; i < 10; i++)
{
//for loop
//numberString = invalue.ToString().ToCharArray()[i]);
// gets the current letter at position "i"
currentLetter = array[i];
switch (currentLetter) //case switch for possible entries
{
case '0':
pnumber = 0; Console.Write(pnumber); break;
case '1':
pnumber = 1; Console.Write(pnumber); break;
case '2':
case 'a':
case 'A':
case 'b':
case 'B':
case 'c':
case 'C':
pnumber = 2; Console.Write(pnumber); break;
case '3':
case 'd':
case 'D':
case 'e':
case 'E':
case 'f':
case 'F':
pnumber = 3; Console.Write(pnumber); break;
case '4':
case 'g':
case 'G':
case 'h':
case 'H':
case 'i':
case 'I':
pnumber = 4; Console.Write(pnumber); break;
case '5':
case 'j':
case 'J':
case 'k':
case 'K':
case 'l':
case 'L':
pnumber = 5; Console.Write(pnumber); break;
case '6':
case 'm':
case 'M':
case 'n':
case 'N':
case 'o':
case 'O':
pnumber = 6; Console.Write(pnumber); break;
case '7':
case 'p':
case 'P':
case 'q':
case 'Q':
case 'r':
case 'R':
case 's':
case 'S':
pnumber = 7; Console.Write(pnumber); break;
case '8':
case 't':
case 'T':
case 'u':
case 'U':
case 'v':
case 'V':
pnumber = 8; Console.Write(pnumber); break;
case '9':
case 'w':
case 'W':
case 'x':
case 'X':
case 'y':
case 'Y':
case 'z':
case 'Z':
pnumber = 9; Console.Write(pnumber); break;
case ' ':
Console.WriteLine();
Console.WriteLine("Bad Value, do not use spaces");
Console.Write("What is the 10 digit phone number? ");
invalue = Console.ReadLine();
numberString = invalue;
array = numberString.ToCharArray();
i = 0;
break;
default:
Console.WriteLine();
Console.WriteLine("Bad Value, use only numbers or letters.");
Console.Write("What is the 10 digit phone number? ");
invalue = Console.ReadLine();
numberString = invalue;
array = numberString.ToCharArray();
i = 0;
break;
}
}
Console.WriteLine();
Console.WriteLine("Press any key to close this window");
Console.ReadLine();
}
}
}
感谢您让我们开始修复,我们都很感激,朋友!
for
行中的分号导致了您的问题:
for (i = 0; i < array.Length; i++) ; //for loop
for 循环后面应该跟一个块,但在这种情况下,因为它后面跟着一个分号,后面的代码不是循环的一部分,而只是执行一次的常规代码。
所以for
循环执行时是一个空体,当它完成时,i
有一个数组长度的值,当然比最大索引高一个,抛出异常。
要解决此问题,请围绕要包含在循环中的代码组创建一个块(大括号)。
这也带来了一个单独的问题 - for
循环通常会在循环本身中声明索引器 - 如果你在 for
中定义了 i
,那么它会已经超出以下几行的范围,并且会更容易看到您的错误,因为它本来是编译器错误。
for (int i = 0; i < array.Length; i++)
{
// Your code here
}
// i is no longer in scope