【发布时间】:2016-02-26 03:15:29
【问题描述】:
我有以下(非常简单的)控制台计算器,可以进行基本的圆计算:
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
}
}
计算后,程序会询问用户是否希望进行另一次计算。如果用户输入 Y,程序会提示他们再次输入半径。如果用户输入 N,则程序终止。
但是,有两个基本问题:
- 如果用户选择按 Y 进行另一次计算,程序会提示用户输入一个值,但也会执行 catch 块并抛出异常。这显示在示例输出中:
输入圆的半径:3
半径为 3 的圆具有以下性质:
面积:28.27
周长:18.85
直径:6
您是否要进行另一个计算 [Y 或 N]?是的
输入圆的半径:[错误]输入的字符串格式不正确。
输入圆的半径:
- 第二个问题是当用户输入除 Y 或 N 以外的其他内容时,程序还会显示意外行为,如输出中所示:
- 输入圆的半径:4
- 半径为 4 的圆具有以下属性:
- 区域:50.27
- 周长:25.13
- 直径:8
- 您是否要进行另一个计算 [Y 或 N]? j 选择无效!按 Y 继续或按 N 退出。
- 您是否要进行另一个计算 [Y 或 N]?无效的选择!按 Y 继续或按 N 退出。
- 您是否要进行另一个计算 [Y 或 N]?无效的选择!按 Y 继续或按 N 退出。
- 是否要进行另一个计算 [Y 或 N]?
我似乎无法弄清楚为什么会发生这两种情况。我怀疑是我使用了goto 和continue,但我说不出来。
【问题讨论】:
-
你真的不应该使用
goto。这很糟糕。 -
感谢您的评论。我知道这不是最好的主意,但在这种情况下似乎很有意义。如果您有其他建议或替代方案,请随时告诉我。
-
函数将是比
goto语句更优雅的解决方案
标签: c# exception console-application