So I'm assuming that all of this code is in a for
loop, and that for
loop is incrementing i
as it's loop statement.
What you can do here is not unconditionally increment i
in each iteration of the loop, and instead only increment i
if you're able to read in the number successfully:
for (int i = 0; i < 10; )
{
try
{
Console.WriteLine("Input number {0}", i + 1);
int number = ReadNumber(start, end);
i++;
}
catch (ArgumentException)
{
Console.WriteLine("The input number wasn't in the alowed range. Please try again.");
}
catch (FormatException)
{
Console.WriteLine("The input isn't an integer number. Please try again.");
}
catch (Exception)
{
Console.WriteLine("An unknown error occured. Please try again.");
}
}
However, for this case I'd say that there's an option that's better still.
What you should really do is, rather than having a method try to get another value from the user and possibly failing and possibly succeeding and needing to be called again, create a method that keeps asking the user for a value until it gets a valid one. This logic is far easier to implement at that scope than it is to try to continually add more iterations to the loop.
Once you create a method that will always get a valid value, trying as hard as it needs to:
public static int ReadNumber(int start, int end, int iteration)
{
while (true)
{
Console.WriteLine("Input number {0}", iteration + 1);
int n;
if (!int.TryParse(Console.ReadLine(), out n))
Console.WriteLine("The input isn't an integer number. Please try again.");
else if (n < start || n > end)
Console.WriteLine("The input number wasn't in the alowed range. Please try again.");
else
return n;
}
}
(Also note the use of conditional checks to determine if the user input is valid, rather than using exceptions for non-exceptional control flow.)
Now our loop is dead easy:
for (int i = 0; i < 10; i++)
{
int number = ReadNumber(start, end, i);
}