级别:新手
我正在用 C# 编写一个控制台程序,用于查找圆形、三角形和梯形的面积。我想通过使用 double.TryParse 确保用户只输入数字。
这是代码:
Console.WriteLine("AreaSolution finds the area of different shapes.");
Console.ReadLine();
ln1: Console.WriteLine("To find area of circle, press c. To find area of triangle press t.For trapezoid, enter tr. ");
var x = Console.ReadLine();
switch (x)
{
case "c":
Console.WriteLine("Enter radius of circle, in centimeters");
var radius = Console.ReadLine();
double rad = Convert.ToDouble(radius);
if (double.TryParse(rad) == false)
{ Console.WriteLine("Numbers only!"); };
Console.WriteLine("AREA: " + 3.14 * rad * rad + " cm sq.");
Console.ReadLine();
break;
case "t":
Console.WriteLine("Enter base, in centimeters");
var bas = Console.ReadLine();
double tbase = Convert.ToDouble(bas);
Console.WriteLine("Enter height, in centimeters");
var tHi = Console.ReadLine();
double tHei = Convert.ToDouble(tHi);
Console.WriteLine("AREA: " + (tbase * tHei / 2) + " cm sq.");
Console.ReadLine();
break;
case "tr":
Console.WriteLine("Enter the length of the top base, in cm");
var tpbas = Console.ReadLine();
double bas1 = Convert.ToDouble(tpbas);
Console.WriteLine("Enter length of bottom base, in cm");
var btmbase = Console.ReadLine();
double bas2 = Convert.ToDouble(btmbase);
Console.WriteLine("Enter height, in cm");
var trHe = Console.ReadLine();
double trH = Convert.ToDouble(trHe);
Console.WriteLine("AREA: " + (bas1 + bas2) * trH / 2 + " cm sq.");
Console.ReadLine();
break;
default:
Console.WriteLine("Please enter a valid character: c for circle, t for triangle, or tr for trapeziod.");
break;
}
Console.WriteLine("Another computation?(Y/N)");
string newComp = Console.ReadLine();
switch (newComp)
{
case "y":
goto ln1;
break;
case "n":
return;
break;
}
但是,在“double.TryParse(rad)”这一行,我得到一个错误:
方法“TryParse”的重载没有一个参数。
我该如何解决这个错误?提前致谢。非常感谢您的帮助。