Based on your description, I make a code example for the guess number game.
I used return to exit the game and used break to retry the game.
Code:
static void Main(string[] args)
{
int attempts = 0;
int answer = 45;
int guess = 12;
bool t = false;
while(attempts<5)
{
while (true)
{
Console.WriteLine("Guess a number between 0~100");
t = int.TryParse(Console.ReadLine(), out guess);
if (t == false)
{
Console.WriteLine("Your input is not a number, please input again, you have {0} changes",4-attempts);
break;
}
else
{
if(guess>100||guess<0)
{
Console.WriteLine("Your input number is not in the range, please input again, you have {0} changes", 4 - attempts);
break;
}
else
{
if(answer==guess)
{
Console.WriteLine("You guessed right");
Console.WriteLine("Game over");
Console.ReadLine();
return;
}
else
{
Console.WriteLine("You gueesed wrong, please input again, you have {0} changes", 4 - attempts);
break;
}
}
}
}
if (attempts == 4)
{
Console.WriteLine("You have no chances");
Console.WriteLine("Game over");
Console.ReadLine();
return;
}
attempts = attempts + 1;
}
}
Tested Result:
