[英]System.FormatException: Input string was not in a correct format. c#
[英]System.FormatException: 'Input string was not in a correct format.' in C# Yellowbook textbook
我正在关注 Rob Miles 在 C# 编程黄书中的一个例子。 我从电子书中复制并粘贴了这个例子。
这是错误:
width = double.Parse(widthString); <- this gives the error of: System.FormatException: 'Input string was not in a correct format.' <- Why???
完整代码:
double width, height, woodLength, glassArea; string widthString, heightString; widthString = Console.ReadLine(); width = double.Parse(widthString); <- this gives the error of: System.FormatException: 'Input string was not in a correct format.' <- Why??? heightString = Console.ReadLine(); height = double.Parse(heightString); woodLength = 2 * (width + height) * 3.25; glassArea = 2 * (width * height); Console.WriteLine("The length of the wood is " + woodLength + " feet"); Console.WriteLine("The area of the glass is " + glassArea + " square metres"); Console.ReadLine(); }
该代码从控制台读取一个字符串。 该字符串应为有效的双精度值(即 1.2、4.3 等),但您似乎输入了无效的双精度值。
我建议添加一些提示来指导用户他们期望输入的内容。 例如:
double width, height, woodLength, glassArea; string widthString, heightString;
Console.Write("Enter the width (as a decimal number): "); // <- Add this
widthString = Console.ReadLine();
width = double.Parse(widthString); <- this gives the error of: System.FormatException: 'Input string was not in a correct format.' <- Why???
Console.Write("Enter the height (as a decimal number): "); // <- And this
heightString = Console.ReadLine();
height = double.Parse(heightString);
woodLength = 2 * (width + height) * 3.25;
glassArea = 2 * (width * height);
Console.WriteLine("The length of the wood is " + woodLength + " feet");
Console.WriteLine("The area of the glass is " + glassArea + " square metres");
Console.ReadLine();
}
干杯,伊恩
如果您的输入字符串为空或非数字(或对于双变量范围而言太大),则 if 将引发该错误。 在这些情况下,我更喜欢使用 TryParse。
double width, height, woodLength, glassArea; string widthString, heightString;
widthString = Console.ReadLine();
heightString = Console.ReadLine();
double.TryParse(widthString, out width);
double.TryParse(heightString, out height);
woodLength = 2 * (width + height) * 3.25;
glassArea = 2 * (width * height);
Console.WriteLine("The length of the wood is " + woodLength + " feet");
Console.WriteLine("The area of the glass is " + glassArea + " square metres");
Console.ReadLine();
如果输入能够被解析,这将输出高度/宽度的更新值。如果不是,它将默认为解析尝试之前设置的任何值。
正如下面的评论中提到的(感谢您的反馈),您还需要验证输入。 你可以通过多种方式做到这一点。 以下是有关如何执行此操作的示例:
double width, height, woodLength, glassArea; string widthString, heightString;
width = GetValidDblInput("Please enter the width:");
height = GetValidDblInput("Please enter the height:");
woodLength = 2 * (width + height) * 3.25;
glassArea = 2 * (width * height);
Console.WriteLine("The length of the wood is " + woodLength + " feet");
Console.WriteLine("The area of the glass is " + glassArea + " square metres");
Console.ReadLine();
}
static double GetValidDblInput(string inputRequest)
{
bool IsValid = false;
double val = 0;
while(!IsValid)
{
Console.WriteLine(inputRequest);
if(double.TryParse(Console.ReadLine(), out val))
{
IsValid = true;
}
else
{
Console.WriteLine("Invalid input. Please enter a valid value (e.g. 1, 3.25, 400)");
}
}
return val;
}
在控制台上输入值时,请确保使用正确的小数点分隔符。 如果在使用解析方法时没有指定特定格式,框架通常会回退到程序运行所在操作系统的区域格式。
如果要使用特定格式,可以使用以下重载之一:
Parse(String, NumberStyles)
Parse(String, IFormatProvider)
Parse(String, NumberStyles, IFormatProvider)
在此处查看文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.