// 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 回圈之后,我將結果輸出到控制臺,但它給出了一個未處理的例外。
uj5u.com熱心網友回復:
我不確定您的 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;
}
}
uj5u.com熱心網友回復:
在你的行
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 個“=”字符。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/331409.html
