在一两年前我第一次做这个的时候,向我扔了一个“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;
}
}
}
}
感谢任何帮助,他一直向我寻求帮助,我一直告诉他他和我一样了解。ps 调试器指向此异常的第 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();
}
}
}
感谢您让我们开始修复,我们都很感激,朋友!