C# 为什么在不应该';不会吧?

C# 为什么在不应该';不会吧?,c#,exception,console-application,C#,Exception,Console Application,我有以下(非常简单)的控制台计算器,可以进行基本的圆计算: using System; namespace Circle { class Program { /* Compute the area of a cricle given its radius. */ public static double Area(double radius) { return Math.Pow(radius, 2) * M

我有以下(非常简单)的控制台计算器,可以进行基本的圆计算:

using System;

namespace Circle
{
    class Program
    {
        /* Compute the area of a cricle given its radius. */
        public static double Area(double radius)
        {
            return Math.Pow(radius, 2) * Math.PI;
        }

        /* Compute the circumference of a circle given is radius. */
        public static double Circumference(double radius)
        {
            return radius * 2 * Math.PI;
        }

        /* Compute the diameter of a circle given its radius. */
        public static double Diameter(double radius)
        {
            return radius * 2;
        }

        /* Report the result. */
        public static string Result(double radius, double area, double circumference, double diameter)
        {
            return "- A circle whose radius is " + radius + " has the following properties: "
                    + "\n- Area: " + area.ToString("0.##") + "\n- Circumference: " + circumference.ToString("0.##")
                    + "\n- Diameter: " + diameter.ToString("0.##");
        }

        static void Main(string[] args)
        {
            double radius = 0;
            char choice;
            while (true)
            {
            Calculate:
                {
                    // 1. Get the radius from the user. 
                    Console.Write("- Enter the radius of the circle: ");
                    try
                    {  // verify the input is of numerical type 
                        radius = Convert.ToDouble(Console.ReadLine());
                        if (radius <= 0)  // check for negative values 
                        {
                            Console.WriteLine(" [Error] Radius must be a positive value!");
                            Console.WriteLine();
                            continue;  // restart from the next iteration without executing the rest of the statements 
                        } // end if 
                    }
                    catch (FormatException e)
                    {
                        Console.WriteLine(" [Error] " + e.Message);
                        Console.WriteLine(); // skip a line
                        continue;  // restart from the next iteration without executing the rest of the statements 
                    } // end catch 
                }
                // 2. Calculate the area, circumference, and diameter of the circle. 
                double area = Area(radius);
                double circumference = Circumference(radius);
                double diameter = Diameter(radius);
                // 3. Display the results. 
                Console.WriteLine(Result(radius, area, circumference, diameter));
            // 4. Ask the user whether to quit.
            Ask:
                {
                    Console.Write("- Do you wish to make another calculation [Y or N]? ");
                    choice = Convert.ToChar(Console.Read());
                }
                if (choice.Equals('Y') || choice.Equals('y'))
                {
                    goto Calculate; // return to the beginning of the Calculate block. 
                }
                else if (choice.Equals('N') || choice.Equals('n'))
                {
                    break; // exit 
                }
                else {
                    Console.WriteLine("Invalid choice! Press Y to continue or N to exit.");
                    goto Ask; // return to the beginning of the Ask block. 
                }
            }  // end while 
            Console.WriteLine("Thank you for using me. Have a nice day!");
            Console.WriteLine();
        } // end Main 
    }
}
使用系统;
名称空间圈
{
班级计划
{
/*计算给定半径的圆环的面积*/
公共静态双区(双半径)
{
返回Math.Pow(半径,2)*Math.PI;
}
/*计算给定半径的圆的周长*/
公共静态双周长(双半径)
{
返回半径*2*Math.PI;
}
/*计算给定半径的圆的直径*/
公共静态双直径(双半径)
{
返回半径*2;
}
/*报告结果*/
公共静态字符串结果(双半径、双面积、双周长、双直径)
{
return”-半径为“+radius+”的圆具有以下属性:
+“\n-区域:“+Area.ToString(“0.\35;”+“\ n-圆周:”+圆周.ToString(“0.\35;#”“”)
+“\n-Diameter:”+Diameter.ToString(“0.###”);
}
静态void Main(字符串[]参数)
{
双半径=0;
字符选择;
while(true)
{
计算:
{
//1.从用户处获取半径。
控制台。写(“-输入圆的半径:”);
尝试
{//验证输入是否为数字类型
radius=Convert.ToDouble(Console.ReadLine());

如果(radius,问题在于使用
Read()
ReadLine()
,以及如何将输入从shell发送到
控制台
对象

在Console对象中,输入流(缓冲区)仅在按下[Enter]后加载。此时,
Read()
将返回字符串中的下一个未读字符,但仅返回该字符。当
ReadLine()时
随后被调用,它将进入换行符仍在等待的缓冲区,因此它立即返回(在本例中)一个空字符串。欢闹随之而来


修正留给读者作为练习;)

问题在于您的
Convert.ToChar(Console.Read())
。它从输入中读取字符,但在按下enter键之前不会从控制台设置输入。因此
Console.Read()
正确地获取
'Y'
,但是输入有一个ENTER排队,因此您的
Convert.ToDouble(Console.ReadLine())
获取一个它试图转换的空字符串,因此出现异常

Convert.ToChar(Console.Read())
更改为
Convert.ToChar(Console.ReadLine())
,即可正常工作

您还应该摆脱
goto
s,也应该摆脱异常捕获-您应该改用
double.TryParse(…)
。不再有异常


我对您的代码进行了重构,以供您尝试-无
goto
s,无异常处理

while (true)
{
    while (true)
    {
        Console.Write("- Enter the radius of the circle: ");
        double radius;
        if (double.TryParse(Console.ReadLine(), out radius) && radius > 0.0)
        {
            double area = Area(radius);
            double circumference = Circumference(radius);
            double diameter = Diameter(radius);
            Console.WriteLine();
            Console.WriteLine(Result(radius, area, circumference, diameter));
            break;
        }
        Console.WriteLine(" [Error] Radius must be a positive value!");
        Console.WriteLine();
    }
    string choice = "";
    while (true)
    {
        Console.Write("- Do you wish to make another calculation [Y or N]? ");
        choice = Console.ReadLine();
        if (new [] { "Y", "N", }.Contains(choice.ToUpper()))
        {
            break;
        }
        Console.WriteLine();
        Console.WriteLine("- Invalid choice! Press Y to continue or N to exit.");
    }
    if (choice.ToUpper() == "N")
    {
        break;
    }
    Console.WriteLine();
}
Console.WriteLine();
Console.WriteLine("- Thank you for using me. Have a nice day!");
Console.WriteLine();

你真的不应该使用
goto
。这很糟糕。谢谢你的评论。我知道这不是最好的主意,但对于这种情况来说似乎很有意义。如果你有其他建议或选择,请随时告诉我。函数将是一个比
goto
语句更优雅的解决方案。如果你的作业指的是我遇到的问题作为一名C#初学者,我为自己选择了,那么你是对的。谢谢你的输入!足够公平!我记得我自己也对一些语言做了同样的操作。经过编辑。不用担心。再次感谢:)这完全解决了问题。我承认,我对ReadLine()和Read()之间区别的理解是最基本的。毫不奇怪我在那里做了什么!谢谢!只是对重构版本的一点了解:我把它复制到我的IDE中进行测试,但它告诉我没有“Contains”方法,因此它给了我一个错误。我在VS 2015中使用C#6.0(不过我得到了修改程序背后的一般逻辑)您需要一个对
System.Linq
的引用,我认为
包含的
才能工作。