【发布时间】:2020-10-03 15:14:26
【问题描述】:
我正在使用 Rob Miles 的 C# 编程黄皮书学习 C#。在本文中,他讨论了一个计算器示例,用于确定所需的总玻璃面积(以平方米为单位)。我重新创建了这段代码,并想尝试用对我有意义的东西来改进它。例如,在他的代码中,他使用了很多看起来对我没有吸引力的空行。我想试着让它看起来更干净一点。我主要是想去掉第一个空行,但要确保空行出现在下一行。
这是原始代码。
double width, height, woodLength, glassArea;
const double MAX_WIDTH = 5.0;
const double MIN_WIDTH = 0.5;
const double MAX_HEIGHT = 3.0;
const double MIN_HEIGHT = 0.75;
string widthString, heightString;
do {
Console.Write("\nGive the width of the window between " + MIN_WIDTH + " and " + MAX_WIDTH + ": ");
widthString = Console.ReadLine();
width = double.Parse(widthString);
if (width < MIN_WIDTH){
Console.WriteLine("Width is too small.");
}
if (width > MAX_WIDTH){
Console.WriteLine("Width is too large.");
}
} while (width < MIN_WIDTH || width > MAX_WIDTH);
我尝试使用 goto,但因为循环是不同的方法(?)它不起作用。通过一些谷歌搜索,我发现 goto 是非常糟糕的做法,但我想不出任何其他方法可以使用。 (我也不知道)
Console.Write("Give the width of the window between " + MIN_WIDTH + " and " + MAX_WIDTH + ": ");
goto first_loop;
do {
Console.Write("\nGive the width of the window between " + MIN_WIDTH + " and " + MAX_WIDTH + ": ");
first_loop:
widthString = Console.ReadLine();
width = double.Parse(widthString);
if (width < MIN_WIDTH){
Console.WriteLine("Width is too small.");
}
if (width > MAX_WIDTH){
Console.WriteLine("Width is too large.");
}
} while (width < MIN_WIDTH || width > MAX_WIDTH);
【问题讨论】:
-
原文中没有看到空行
-
@Stefan,提示符开头是
\n。 -
我的意思是循环中的 \n 。我希望它第一次出现时它不存在,但是当它因为不正确的数字而循环时应该有一个 \n 所以它是分开的