I can't figure out what I am doing wrong. What I am trying to do is get user input and validate that it is a number, between 1-4, and nothing else crashes the program. But I can't get the tryparse number validation to work. It wont run the code inside. Any integers outside of 1 and 5 don't throw errors, but entering decimals, strings, etc., do. Its an obvious fix but I cant seem to figure it out.
class Program
{
static void Main(string[] args)
{
string[] ColorsArray = new string[12] { "blue", "red", "green", "yellow", "blue", "green", "blue", "yellow", "red", "green", "red", "yellow" };
float[] LengthArray = new float[12] { 1.3f, 1.4f, 5.6f, 1.5f, 3.5f, 5.4f, 1.2f, 6.5f, 4.4f, 4.1f, 3.3f, 4.9f };
Console.WriteLine("Select a color of Fish by entering the corresponding number \r\n 1. Blue \r\n 2. Yellow \r\n 3. Red \r\n 4. Green");
string ColorChoiceNumber = Console.ReadLine();
int ColorChoiceNumberInt = int.Parse(ColorChoiceNumber);
if (ColorChoiceNumberInt == 1)
{
Console.WriteLine("The Biggest Blue Fish is Fish Number ");
}
else if (ColorChoiceNumberInt == 2)
{
Console.WriteLine("The Biggest Yellow Fish is Fish Number");
}
else if (ColorChoiceNumberInt == 3)
{
Console.WriteLine("The Biggest Red Fish is Fish Number");
}
else if (ColorChoiceNumberInt == 4)
{
Console.WriteLine("The Biggest Green Fish is Fish Number");
}
else if (!(int.TryParse(ColorChoiceNumber, out ColorChoiceNumberInt)) && ColorChoiceNumberInt < 1 && ColorChoiceNumberInt > 4)
{
Console.WriteLine("Please only enter a number. It must be 1-4.");
ColorChoiceNumber = Console.ReadLine();
int.TryParse(ColorChoiceNumber, out ColorChoiceNumberInt);
}
else
{
Console.WriteLine("Please only enter a number. It must be 1-4.");
ColorChoiceNumber = Console.ReadLine();
int.TryParse(ColorChoiceNumber, out ColorChoiceNumberInt);
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire