【发布时间】: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中测试<200时,您无需在下一个else if中测试>=200。现在一个 200 的值落在裂缝之间! (400、600、800 相同)
标签: c# if-statement while-loop