vendredi 22 octobre 2021

Wanting program to output largest user input number that is below 800. I have the code nested in a while loop but it is outputting a higher number [closed]

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

Console.WriteLine("The Largest Bacteria Is: " + largestValue); Console.WriteLine("The Smallest Bacteria Is: " + smallestValue);

The output is :

The Largest Bacteria Is: 900
The Smallest Bacteria Is: 67

The largest bacteria should not be able to be over 800 as it is nested in the while loop at the beginning?

Aucun commentaire:

Enregistrer un commentaire