5

级别:新手

我正在用 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”的重载没有一个参数。

我该如何解决这个错误?提前致谢。非常感谢您的帮助。

4

3 回答 3

5
            double rad = Convert.ToDouble(radius);

            if (double.TryParse(rad) == false)

应该

            double rad;
            if (!double.TryParse(radius, out rad))

使用!(不)而不是== false更好的编码实践。

此外,TryParse 需要一个out值(它进行解析,不需要Convert.ToDoubleand ...Convert.ToDouble如果它不可解析,您将出错 - 糟糕!)

于 2013-06-29T02:42:50.040 回答
3

TryParse 返回一个布尔值,并使用out参数返回解析后的值

double rad;
if (!double.TryParse(radius, out rad))
{ 
    Console.WriteLine("Numbers only!"); 
}
于 2013-06-29T02:43:04.417 回答
1
double result;

if (double.TryParse("123.45", out result))
{
    Console.WriteLine(result);
}

有两个TryParse()方法重载

如果您需要将指定样式和文化特定格式的数字的字符串表示形式转换为其等效的双精度浮点数,那么您应该使用重载函数:

if (double.TryParse("2.3", NumberStyles.Number, CultureInfo.CurrentCulture, out result))
{
    Console.WriteLine(result);
}

if (double.TryParse("3.4", NumberStyles.Any, CultureInfo.GetCultureInfo("en-US"), out result))
{
    Console.WriteLine(result);
}

if (double.TryParse("5,6", NumberStyles.Any, CultureInfo.GetCultureInfo("fr-CA"), out result))
{
    Console.WriteLine(result);
}

if (double.TryParse("7.8", NumberStyles.Any, CultureInfo.InvariantCulture, out result))
{
    Console.WriteLine(result);
}
于 2013-06-29T02:43:32.200 回答