我需要拒絕在我的控制檯應用程序中寫入字符串的能力,此時,當輸入文本而不是數字時,控制檯崩潰。拒絕字符串變量中的字符串
我沿着這一權利的行現在的東西
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);' –