class Program
{
private static int check (int cs)
{
switch (cs)
{
case 3:
return 3;
case 4:
return 4;
case 9:
return 9;
default:
return 0;
}
}
private static int checkCase ()
{
Console.WriteLine("Choose denominator: 3, 4 or 9");
int cs = int.Parse(Console.ReadLine());
int d = check(cs);
if (d == 0)
{
Console.WriteLine("Error");
checkCase(); // <--- BUG
}
return d;
}
static void Main(string[] args)
{
Console.WriteLine("Enter beginning of interval");
int a = int.Parse(Console.ReadLine());
Console.WriteLine("Enter end of interval");
int n = int.Parse(Console.ReadLine());
int d = checkCase();
for (int i = a; i <= n; i++)
{
if (i % d == 0)
Console.WriteLine(i);
}
}
}
This simple program checks which numbers from a given interval divide to 3, 4 or 9. I'm trying to make a Switch Case menu to choose from the denominators. I managed to do that but I wanted to make it so that the default case resets the whole switch statement so I made a seperate function for it, where it resets at line 28 if the default is selected.
But I get some kind of a bug (if i may call it that) when I choose the default it checks it for 0 then resets the function as expected and after I select one of the other cases it sets the variable "d" to that case as it should, and goes to return, here's the bug now, but instead of continuing to the main method it goes back to line 28 and resets the variable "d" back to 0 and the variable "cs" back to the default case and then goes to main method and sends an error because d=0.
I want to know why is that "bug" occuring. And If there's a simpler way to restart the switch statement I'd be happy if you share.
Thank you.