如何防止用户输入为整数或字符串?

时间:2019-06-19 02:56:57

标签: c# user-input

我对C#编程非常陌生,我正在尝试建立一个基本的用户输入方案,以练习处理不正确的用户输入,例如以数字表示的名称或以数字表示的名称,并且似乎无法在线找到任何结果。 / p>

//User Name
Console.WriteLine("Welcome to your journey traveler, what is 
your name?");
userName = Console.ReadLine();

while (string.IsNullOrEmpty(userName))
{
Console.WriteLine("No name? every great hero has a title! 
Try once more...");
userName = Console.ReadLine();
}
Console.WriteLine("Welcome " +userName + "!");

//User Age
Console.WriteLine("Now " +userName + ", How old are you?");
userAge = Int32.Parse(Console.ReadLine());

//If statement to prevent string as input
if(Int32.TryParse(**I dont know what to put in here!!!**))

似乎有一种方法可以防止将int输入作为字符串,但不能将string作为int防止,因为据我了解,所有数字在转换之前都被视为字符。

0 个答案:

没有答案