Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为第一次迭代跳过循环中的第一步_C#_For Loop_Goto - Fatal编程技术网

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

C# 为第一次迭代跳过循环中的第一步,c#,for-loop,goto,C#,For Loop,Goto,我正在学习C#使用Rob Miles的C#programming黄皮书。在这篇文章中,他正在讨论一个计算器示例,用于确定所需的总玻璃面积(以平方米为单位)。我重新创建了这段代码,并想尝试用对我有意义的东西来改进它。例如,在他的代码中,他使用了很多空白行,这对我来说并不吸引人。我想让它看起来更干净一点。我主要想消除这个第一个空白行,但是确保空白行出现在下一个空白行。 这是原始代码 double width, height, woodLength, glassArea; const

我正在学习C#使用Rob Miles的C#programming黄皮书。在这篇文章中,他正在讨论一个计算器示例,用于确定所需的总玻璃面积(以平方米为单位)。我重新创建了这段代码,并想尝试用对我有意义的东西来改进它。例如,在他的代码中,他使用了很多空白行,这对我来说并不吸引人。我想让它看起来更干净一点。我主要想消除这个第一个空白行,但是确保空白行出现在下一个空白行。 这是原始代码

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);
双倍宽度、高度、林地长度、面积;
const double MAX_WIDTH=5.0;
常数双最小宽度=0.5;
const double MAX_HEIGHT=3.0;
常数双最小高度=0.75;
字符串宽度字符串,高度字符串;
做{
控制台。写入(“\n将窗口宽度设置在“+MIN_width+”和“+MAX_width+”:”)之间;
widthString=Console.ReadLine();
宽度=double.Parse(宽度字符串);
如果(宽度<最小宽度){
控制台。WriteLine(“宽度太小”);
}
如果(宽度>最大宽度){
Console.WriteLine(“宽度太大”);
}
}而(宽度<最小宽度>最大宽度);
我试着使用goto,但因为循环是另一种方法(?)它不起作用。通过一些谷歌搜索,我发现goto是一种非常糟糕的做法,但我想不出还有其他方法可以使用。 (我也不知道)

Console.Write(“给出介于“+MIN\u width+”和“+MAX\u width+”:”)之间的窗口宽度;
转到第一个循环;
做{
控制台。写入(“\n将窗口宽度设置在“+MIN_width+”和“+MAX_width+”:”)之间;
第一个循环:
widthString=Console.ReadLine();
宽度=double.Parse(宽度字符串);
如果(宽度<最小宽度){
控制台。WriteLine(“宽度太小”);
}
如果(宽度>最大宽度){
Console.WriteLine(“宽度太大”);
}
}而(宽度<最小宽度>最大宽度);
我主要想消除第一个空行,但要确保 下一行将显示空白行

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

如果您也不希望像@Idle\u 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);
bool isFirstRun=true;
做
{
如果(!isFirstRun)Console.WriteLine();
isFirstRun=false;
控制台。写入(“给出介于“+MIN_width+”和“+MAX_width+”:”)之间的窗口宽度;
widthString=Console.ReadLine();
宽度=double.Parse(宽度字符串);
如果(宽度<最小宽度)
{
控制台。WriteLine(“宽度太小”);
}
如果(宽度>最大宽度)
{
Console.WriteLine(“宽度太大”);
}
}而(宽度<最小宽度>最大宽度);

我在列表中没有看到空行original@Stefan,它是提示符开头的
\n
。我指的是循环中的\n。我希望它第一次出现时不在那里,但是当它因为错误的数字而循环时,应该有一个\n,它就是这样separated@Idle_Mind:谢谢。。。错过了那个XD。。。另一个选择是在末尾加一个
WriteLine
。虽然我没有戴眼镜,但我今天倾向于过度看东西。对……这取决于如果值在范围内,他们是否不需要额外的一行。描述中没有提到这一点。或者使用
bool
表示您在第一次迭代中,在循环中将其设置为false。有几种方法可以解决这个问题。谢谢你这么说,第一个解决方案似乎显而易见。我还研究了你的方法,并试图弄清楚它是如何工作的。我想知道你是否可以为我澄清一些事情,如果我做对了-是否有必要在一开始就将valid指定为false,还是仅仅是一种好的做法如果我理解正确,double.TryParse会尝试将字符串解析为double,它会在此处返回true或false值。如果为真,则使用out以宽度显示结果?-((宽度<最小宽度)?“小”:“大”)什么是最大宽度?和:在这个上下文中是什么意思?是的,所以不需要设置它
TryParse
基于解析返回true/false,并将数值放入
out
参数中。现在,当用户键入完全不是数字的内容时,您可以显示一条漂亮的消息,而不是程序崩溃。这里的
if…else
语句的缩写。
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);
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);