在一两年前我第一次做这个时没有的地方向我抛出"System.IndexOutOfRangeException"。我问了我现在的教授,但他只是说"行得通,没有问题"。我们都得到了例外,所以我想知道为什么老师没有。这是代码:
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