C# c语言中的数字字符串验证#
我试图验证“columnsValidation”是否为数字字符串,如果是,则将其转换为int 由于某种原因,我最终陷入了一个无休止的循环,因为“isNumber”总是等于false 这段代码是我彩票项目的一部分 我希望我的问题足够清楚,如果需要更多信息,请告诉我,我会回答 提前感谢,, 宜兰C# c语言中的数字字符串验证#,c#,string,boolean,tryparse,C#,String,Boolean,Tryparse,我试图验证“columnsValidation”是否为数字字符串,如果是,则将其转换为int 由于某种原因,我最终陷入了一个无休止的循环,因为“isNumber”总是等于false 这段代码是我彩票项目的一部分 我希望我的问题足够清楚,如果需要更多信息,请告诉我,我会回答 提前感谢,, 宜兰 更正您对TryParse的使用: isNumber = int.TryParse(columnsValidation, out columns); TryParse返回布尔值,指示解析是否成功,如果成功,
更正您对
TryParse
的使用:
isNumber = int.TryParse(columnsValidation, out columns);
TryParse
返回布尔值,指示解析是否成功,如果成功,则将设置为带有解析结果的参数。更正TryParse
的用法:
isNumber = int.TryParse(columnsValidation, out columns);
TryParse
返回布尔值,指示解析是否成功,如果成功,则将out
参数设置为解析结果。您需要将int.TryParse
与columnsValidation
if (!int.TryParse(columnsValidation,out columns)
{
Console.WriteLine("You've inserted an invalid value, please try again.");
columnsValidation = Console.ReadLine();
}
else
{
isNumber = true;
}
您需要将int.TryParse
与columnsValidation一起使用
if (!int.TryParse(columnsValidation,out columns)
{
Console.WriteLine("You've inserted an invalid value, please try again.");
columnsValidation = Console.ReadLine();
}
else
{
isNumber = true;
}
为什么不使用Int.TryParse
int columns = 0;
while(true)
{
if (!Int32.TryParse(columnsValidation,out columns)
{
Console.WriteLine("You've inserted an invalid value, please try again.");
columnsValidation = Console.ReadLine();
}
else
{
break;
}
}
为什么不使用Int.TryParse
int columns = 0;
while(true)
{
if (!Int32.TryParse(columnsValidation,out columns)
{
Console.WriteLine("You've inserted an invalid value, please try again.");
columnsValidation = Console.ReadLine();
}
else
{
break;
}
}
您的代码正在寻找true
作为输入…bool.TryParse尝试从'true'或'false'中获取bool,并且不告诉您columsValidaton是否为数字。您使用bool.TryParse
的方式不正确。如果您不打算使用它的输出,那么您最好使用bool.Parse(columnsValidation)
您的代码寻找true
作为输入…bool.TryParse尝试从'true'或'false'中获取bool,并且不告诉您columnsvalidaton是否是一个数字。您使用bool.TryParse
的方式不正确。如果不打算使用它的输出,那么最好使用bool.Parse(columnsValidation)