“使用double.TryParse时,方法'没有重载'TryParse'接受一个参数”错误

时间:2013-06-29 02:41:05

标签: c#

等级:新手

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

如何修复此错误? 提前致谢。非常感谢您的帮助。

3 个答案:

答案 0 :(得分:3)

            double rad = Convert.ToDouble(radius);

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

应该是

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

使用!(而非)代替== false,就像更好的编码习惯一样。

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

答案 1 :(得分:3)

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

double rad;
if (!double.TryParse(radius, out rad))
{ 
    Console.WriteLine("Numbers only!"); 
}

答案 2 :(得分: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);
}