dimanche 26 mai 2019

How can I make sure that a certain line is printed only when the if statement is met in a while-loop

I am trying to print an if statement but only if the values are correct and they meet the requirements. However, the if statement is inside of a while loop and even though it asks the user to try again when the value in the if statement is not correct it still prints out the numbers the user inputted the first time in the WriteLine outside of the if statement (where it says SIMULATION). I want it to ask the user to try again until the values meet the required standard of the if statement.

namespace Woodchuck { class Program { static void Main(string[] args) { string restart = "Y";

    while(restart == "Y")
    {
    int totalWoodChucks = 0;
    int days = 0;

    const int MAXDAYS = 10;
    const int MINDAYS = 1;
    const int MAXWOOD = 100;
    const int MINWOOD = 1;


Write("Enter number of woodchucks for this simulation (1-100): ");
int.TryParse(ReadLine(), out totalWoodChucks);

if(totalWoodChucks > MAXWOOD || totalWoodChucks < MINWOOD)
{
    WriteLine("Please try again");
}


Write("Enter number of days for this simulation (1-10): ");
int.TryParse(ReadLine(), out days);
    if(days > MAXDAYS || days < MINDAYS)
{
WriteLine("Please try again");
}

WriteLine($"SIMULATION 1: {totalWoodChucks} woodchucks over {days} days");

Write("\nTo run another simulation, enter 'Y':"); restart = ReadLine().ToUpper();

    }
//Debug pause
WriteLine("Press any key to wrap it up...");
    }
}

}

Aucun commentaire:

Enregistrer un commentaire