我有以下(非常简单的)控制台计算器进行基本圆计算:
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,则程序终止。
然而,有两个基本问题:
- 输入圆的半径:3
- 半径为3的圆具有以下属性:
- 面积:28.27
- 周长:18.85
- 直径:6
- 您是否想进行另一次计算[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
的使用,但我无法分辨。
答案 0 :(得分:3)
问题在于你Convert.ToChar(Console.Read())
。它从输入中读取一个字符,但输入不会从控制台设置,直到按下enter键。因此Console.Read()
正确获取'Y'
,但输入的 ENTER 排队等待,因此您的Convert.ToDouble(Console.ReadLine())
获取一个它尝试转换的空字符串,因此异常。
将Convert.ToChar(Console.Read())
更改为Convert.ToChar(Console.ReadLine())
,效果正常。
您还应该摆脱goto
并摆脱异常陷阱 - 您应该使用double.TryParse(...)
代替。没有更多的例外。
我对您的代码进行了重构,供您尝试 - 没有goto
而不是异常处理。
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();
答案 1 :(得分:0)
问题在于您使用Read()
和ReadLine()
,以及输入如何从shell发送到Console
对象。
在Console对象中,仅在按下[Enter]后才加载输入流(缓冲区)。此时,Read()
将返回字符串中的下一个未读字符,但仅返回一个字符。当随后调用ReadLine()
时,它会转到换行符仍在等待的缓冲区,因此它会立即返回(在本例中)一个空字符串。随之欢闹。
补救是留给读者的练习;)