我需要拒绝在我的控制台应用程序中写入字符串的能力,此时,当输入文本而不是数字时,控制台崩溃。拒绝字符串变量中的字符串
我沿着这一权利的行现在的东西
class Program
{
static void Main(string[] args)
{
string[] names = new string[2];
string age;
bool agetest = false;
Console.WriteLine("Hello, I am the NameBot2000, What is your first name?");
names[0] = Console.ReadLine();
Console.WriteLine("Well done. What is your surname?");
names[1] = Console.ReadLine();
Console.WriteLine("What year were you born in?");
age = Console.ReadLine();
int.Parse(age);
if (Enumerable.Range(0,2015).Contains(age));
int year = 0;
string wow = "";
if (Enumerable.Range(0,31).Contains(year))
wow = "young";
else if (Enumerable.Range(31,51).Contains(year))
wow = "old";
else if (Enumerable.Range(51,500).Contains(year))
wow = "ancient";
Console.WriteLine("Well done. You said your name was {0} {1}, and you are {2} years old!", names[0], names[1], year);
Console.WriteLine("You are so {0}!", wow);
Console.ReadLine();
}
}
我试图纳入一个布尔值,但我不能确定如何比较变量来检查它是哪一种格式。
谢谢堆提前!
http://www.dotnetperls.com/int-parse – Deftwun
有一个*很多*与此代码的问题,因此很难准确地确定你问哪一个。你能否把这个缩小到一个小片段来展示你确切的问题? – Blorgbeard
'int.Parse(age);'不会做任何事情。将结果保存在'int'中。 'int iage = int.Parse(age);' –