lundi 21 décembre 2020

Try/catch in for-loop

I'm new to programming and trying to solve a problem. I've got an array I want to fill with input from a user but I want to make sure it's only integers between 1-25 and no string input. How do I do this?

The code I've gotten this far is this:

for (int i = 0; i < lottery.Length; i++)
{
    Console.Write(i + ": ");

    try
    {
        input = int.Parse(Console.ReadLine());
        lottery[i] = input;
    }
    catch
    {
        Console.WriteLine("Only integers!");
        i--;
    }
}

This codes makes sure the only input to the array is integers but how do I get the user to only write numbers between 1-25? I've tried diffrent if-statements and other loops 3 hours but can't solve it. Please help.

Aucun commentaire:

Enregistrer un commentaire