【问题标题】:Skipping the first step in a loop for the first iteration第一次迭代跳过循环中的第一步
【发布时间】: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 所以它是分开的

标签: c# for-loop goto


【解决方案1】:

我主要想删除第一个空行,但要确保 下一行会出现空行。

从提示中删除\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);

【讨论】:

  • ... 我猜另一种选择就是在最后加上WriteLine。虽然我没有戴眼镜,但我今天倾向于过度看东西。
  • 对...只是取决于如果值在范围内,他们是否不想要额外的行。描述中没有提到这一点。
  • 或使用bool 表示您处于第一次迭代中,在循环中将其设置为false。有几种解决方法。
  • 谢谢你,第一个解决方案似乎很明显,既然你说了。我还查看了您的方法并尝试弄清楚它是如何工作的。我想知道你是否可以为我澄清一些事情,如果我做对了 - 这里是否有必要在一开始就将有效的权利分配为错误的权利,或者只是一种好的做法? - 如果我理解正确,double.TryParse 会尝试将字符串解析为双精度,它会在此处返回真或假值。如果是真的,你用 out 把结果放在宽度上? - ((width
  • default for bool is false,所以不需要设置。 TryParse 根据解析返回真/假,并将数值放入out 参数中。现在,当用户输入完全不是数字的内容时,您可以显示一条好消息,而不是程序崩溃。这里的?conditional operator,只是if...else 语句的简写。
【解决方案2】:

如果您也不想像@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);

【讨论】:

    猜你喜欢
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2013-10-03
    • 2021-12-26
    • 2022-01-07
    • 2010-12-28
    • 2018-09-18
    • 2011-08-08
    相关资源
    最近更新 更多