【问题标题】:Want my while loop to finish and output after I enter a number which is over 800 but it is giving me unhandled exceptions希望我的 while 循环在我输入一个超过 800 的数字后完成并输出,但它给了我未处理的异常
【发布时间】:2021-12-09 01:28:28
【问题描述】:

// While 循环 & if 语句

while (userInput < 800)
{
    if (userInput >0 && userInput <200)
    {
        cOne++;
        sumLengthOne = sumLengthOne + userInput;
        userInput = int.Parse(Console.ReadLine());
    }  
    else if (userInput >200 && userInput <400)
    {
        cTwo++;
        sumLengthTwo = sumLengthTwo + userInput;
        userInput = int.Parse(Console.ReadLine());
    }
    else if (userInput >400 && userInput <600)
    {
        cThree++;
        sumLengthThree = sumLengthThree + userInput;
        userInput = int.Parse(Console.ReadLine());
    }
    else if (userInput >600 && userInput <800)
    {
        cFour++;
        sumLengthFour = sumLengthFour + userInput;
        userInput = int.Parse(Console.ReadLine());
    }
    else if (userInput > 800)
    {
        break;
    }
}        

Console.WriteLine("| {0,-10} | {1,5} | {6,10} | {11,15} |" , "Range" , "Count" , "Sum Lengths" , "Percentage");
Console.WriteLine("==========");
Console.WriteLine("| {0,-10} | {1,5} | {6,10} | {11,15} |" , "<200" + "200 - 399" + "400 - 599");
Console.WriteLine("The Largest Bacteria Is: " + largestValue);
Console.WriteLine("The Smallest Bacteria Is: " + smallestValue);

不想要的结果是未处理的异常 " 未处理的异常。System.FormatException: 索引(从零开始)必须大于或等于零且小于参数列表的大小"

在 while 循环之后,我将结果输出到控制台,但它给出了一个未处理的异常。

【问题讨论】:

  • "while 循环后我将结果输出到控制台" - 此代码应包含在问题中,因为它是触发异常的部分
  • 代码审查:当您在if 中测试&lt;200 时,您无需在下一个else if 中测试&gt;=200。现在一个 200 的值落在裂缝之间! (400、600、800 相同)

标签: c# if-statement while-loop


【解决方案1】:

在你的队伍中

Console.WriteLine("| {0,-10} | {1,5} | {6,10} | {11,15} |" , "Range" , "Count" , "Sum Lengths" , "Percentage");

{6,10} 的意思不是“从字符 6 到 10”,而是“使用参数 #6,格式为 10 字符宽”。而且您没有论点#6(或#11)。您的意思是那里的索引 2 和 3。

固定:

Console.WriteLine("| {0,-10} | {1,5} | {2,10} | {3,15} |" , "Range" , "Count" , "Sum Lengths" , "Percentage");

提示:对于下面的分割线,使用这个:

Console.WriteLine(new string ('=', 33));

输出 33 个 "=" 字符。

【讨论】:

  • 谢谢汉斯,这很有帮助。
【解决方案2】:

我不确定您的 userInput 是如何发挥作用的,来自输入字段或循环值。但是……

输入 200、400、600、800 等输入时,您没有匹配条件。你要么高于或低于800,但不是=== 800

你的条件应该是这样的:

        while (userInput < 800)
        {
            if (userInput >0 && userInput <= 200) // below or equal too
            {
                cOne++;
                sumLengthOne = sumLengthOne + userInput;
                userInput = int.Parse(Console.ReadLine());
            }  
            else if (userInput >200 && userInput <= 400) // below or equal too
            {
                cTwo++;
                sumLengthTwo = sumLengthTwo + userInput;
                userInput = int.Parse(Console.ReadLine());
            }
            else if (userInput >400 && userInput <= 600) // below or equal too
            {
                cThree++;
                sumLengthThree = sumLengthThree + userInput;
                userInput = int.Parse(Console.ReadLine());
            }
            else if (userInput >600 && userInput <= 800) // below or equal too
            {
                cFour++;
                sumLengthFour = sumLengthFour + userInput;
                userInput = int.Parse(Console.ReadLine());
            }
            else if (userInput > 800)
            {
                break;
            }
        }     
当然,如果您想突破 200、400、600 和 800 及以下,则必须以相反的方式进行 - 如下所示:

        while (userInput < 800)
        {
            if (userInput >= 0 && userInput <200) // 0 or above
            {
                cOne++;
                sumLengthOne = sumLengthOne + userInput;
                userInput = int.Parse(Console.ReadLine());
            }  
            else if (userInput >= 200 && userInput <400) // 200 or above
            {
                cTwo++;
                sumLengthTwo = sumLengthTwo + userInput;
                userInput = int.Parse(Console.ReadLine());
            }
            else if (userInput >= 400 && userInput <600) // 400 or above
            {
                cThree++;
                sumLengthThree = sumLengthThree + userInput;
                userInput = int.Parse(Console.ReadLine());
            }
            else if (userInput >= 600 && userInput <800) // 600 or above
            {
                cFour++;
                sumLengthFour = sumLengthFour + userInput;
                userInput = int.Parse(Console.ReadLine());
            }
            else if (userInput >= 800) // 800 or above
            {
                break;
            }
        }   

【讨论】:

    猜你喜欢
    • 2022-11-16
    • 2023-02-04
    • 2014-11-27
    • 2016-02-29
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-18
    • 1970-01-01
    相关资源
    最近更新 更多