我在练习c#的数组。我做到了这一点,但遗憾的是最终它并不奏效。我想有这样的:将字符串转换为数组以供数组使用?
例如,用户键入“第三”。我希望它在int中被转换为“2”,所以电脑会选取第三个输入的数字。正如我编写它,它现在崩溃。
Console.WriteLine("Please enter 5 numbers of choice.");
Int32[] Names = new Int32[5];
Names[0] = Convert.ToInt32(Console.ReadLine());
Names[1] = Convert.ToInt32(Console.ReadLine());
Names[2] = Convert.ToInt32(Console.ReadLine());
Names[3] = Convert.ToInt32(Console.ReadLine());
Names[4] = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("The number you typed third is " + Names[2]);
Console.Clear();
Console.WriteLine("Which number would you like the computer to remember? first, second, third etc.");
int Choice = Convert.ToInt32(Console.ReadLine());
string ChosenNumber = (Console.ReadLine());
int first = 0;
int second = 1;
int third = 2;
int fourth = 3;
int fifth = 4;
Console.ReadKey();
“它不在最后工作”< - 什么不工作?请具体说明。你期望的输入/输出是什么?你的程序产生了什么? – Neolisk
当您输入int的值时,它崩溃了选择= Convert.ToInt32(Console.ReadLine());当你输入一个字符串值并且它不能被转换为int。然而我希望我的程序自动将字符串“third”转换为int“2”。希望我现在解释清楚 –
您可以使用枚举列表或字典进行转换 – Carl