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。谢谢。
您的问题是您需要拆分转换以测试错误的输入条件。看一下这段代码。
Console.WriteLine("What is the radius of your circle: ");
var isNumber = Double.TryParse(Console.ReadLine(), out double number);
if (!isNumber)
number = 0;
Console.WriteLine("The area of your circle is: " + circleArea(number).ToString());
Console.ReadKey();
这将测试一个合法数字,如果不是,它将仅传递零作为数字。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句