vendredi 22 octobre 2021

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 Loop & if statement

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);

The unwanted result is unhandled exception " Unhandled exception. System.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list"

after the while loop I am outputting the results to console but it gives an unhandled exception instead.

Aucun commentaire:

Enregistrer un commentaire