跳过第一次迭代循环中的第一步

Skipping the first step in a loop for the first iteration

我正在使用 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);

I mainly want to eliminate this first blank line, but make sure the blank line would appear on the next one.

从提示中删除 \n,并将其添加到两条错误消息的末尾:

do
{
    Console.Write("Give 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.\n");
    }
    else if (width > MAX_WIDTH)
    {
        Console.WriteLine("Width is too large.\n");
    }
} while (width < MIN_WIDTH || width > MAX_WIDTH);

不过,如果我自己写这个,我会使用一个布尔值来指示总体成功并在 while 循环条件中检查它。我还会使用 double.TryParse(),如下所示:

bool valid = false;
do
{
    valid = true; // assume good until proven otherwise
    Console.Write("Give the width of the window between " + MIN_WIDTH + " and " + MAX_WIDTH + ": ");
    widthString = Console.ReadLine();
    if (double.TryParse(widthString, out width))
    {
        if (width < MIN_WIDTH || width >MAX_WIDTH )
        {
            valid = false;
            Console.WriteLine("Width is too " + ((width<MIN_WIDTH) ? "small" : "large") + ".");
        }
    }
    else
    {
        Console.WriteLine("Invalid width entry. Please try again.");
        valid = false;
    }
    
    if (!valid)
    {
        Console.WriteLine();
    }
} while (!valid);

如果您也不想像@Idle_Mind 的解决方案那样在末尾添加空白行,您可以这样做

bool isFirstRun = true;
do
{
    if (!isFirstRun) Console.WriteLine();
    isFirstRun = false;

    Console.Write("Give 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);