[英]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.