[英]How can I get my Tryparse working?
我需要使用TryParse
但我真的不知道我想把它放在哪里。 我希望用户能够输入任何值并防止程序崩溃。
string[] userInput = new string[5];
bool isRunning = true;
while (isRunning)
{
Console.WriteLine("[1]Lägg till ett föremål");
Console.WriteLine("[2]Skriv ut innehållet");
Console.WriteLine("[3]Sök i ryggsäcken");
int menyVal = Convert.ToInt32(Console.ReadLine());
switch (menyVal)
{
case 1:
Console.Write("Lägg till ett föremål: ");
userInput[0] = Console.ReadLine();
Console.Write("Lägg till andra föremål: ");
userInput[1] = Console.ReadLine();
Console.Write("Lägg till tredje föremål: ");
userInput[2] = Console.ReadLine();
Console.Write("Lägg till fjärde föremål: ");
userInput[3] = Console.ReadLine();
Console.Write("Lägg till femte föremål: ");
userInput[4] = Console.ReadLine();
break;
case 2:
for (int i = 0; i < userInput.Length; i++)
{
Console.WriteLine(userInput[i]);
}
break;
case 3:
Console.Write("Skriv in sökOrd: ");
string sökOrd = Console.ReadLine();
for (int i = 0; i < userInput.Length; i++)
{
if (userInput[i].ToUpper() == sökOrd.ToUpper())
{
Console.WriteLine(userInput[i]);
break;
}
}
break;
case 4:
isRunning = false;
break;
}
}
Console.ReadLine();
您可以通过使用以下代码行来使用 TryParse
string Input= Console.ReadLine();
int menyVal = 0;
int.TryParse(Input, out menyVal);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.