我有以下的(很簡單)控制檯,做基礎圓計算的計算器:爲什麼當它不應該出現這個異常?
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
輸入圓的半徑:[錯誤]輸入字符串格式不正確。
輸入圓的半徑:
- 當用戶輸入比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'。這不好。 – Enigmativity
感謝您的評論。我知道這不是最好的想法,但對於這種情況來說似乎很合理。如果您有其他建議或替代方案,請隨時通知我。 –
功能將比goto語句更優雅的解決方案 – ohiodoug