【发布时间】:2020-03-09 20:19:20
【问题描述】:
class Program
{
static void Main(string[] args)
{
WriteLine("What is the radius of your circle: ");
WriteLine("The area of your circle is: " +
circleArea(Double.Parse(ReadLine())).ToString());
ReadKey();
}
static double circleArea(double radius = 5.00)
{
return Math.PI * (radius * radius);
}
}
我认为我的设置正确;但是,我收到 System.FormatException 错误:'输入字符串的格式不正确。当没有输入值时,在WriteLine("The area of your circle is: " + circleArea(Double.Parse(ReadLine())).ToString()); 行上。我希望它的默认值为 2。谢谢。
【问题讨论】:
-
Double.Parse(...)需要该值。如果您可能不传递值,则应使用Double.TryParse(...)。 -
@PavelAnikhouski - OP 说“当没有输入任何值时”,所以我假设他们只是按 Enter 键。
-
你必须使用 Double.TryParse()。如果输入有效,则返回 true,否则返回 false...
-
@PavelAnikhouski - OP 说“当没有输入值时”,所以我假设他们只是按 Enter 键。 --- 是的,用户会按下回车键
-
WriteLine("The area of your circle is: " + circleArea(Double.TryParse(ReadLine(), out var value) ? value : 2).ToString());