I am currently a student and just learning C#
. I am trying to implement a code so that you may type exit or restart at any point. I can figure out how to go about it or how to approach it. Again I'm brand new to coding and have no background in it before nay an all help is appreciated. Thank you. I have posted a picture as to what I have.
Asked
Active
Viewed 845 times
-2

Emma
- 27,428
- 11
- 44
- 69
-
Learn Loops, https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/for#structure-of-the-for-statement – Vijay Kumbhoje Aug 07 '20 at 18:05
-
3Please always add the minimal amount of code to reproduce your problem here in the body of the question and not as a image. – ChiefTwoPencils Aug 07 '20 at 18:08
-
You should post the code as text and format it using the code sample { } button. Makes it easier for us to copy code snippets to use. – Pete -S- Aug 07 '20 at 18:19
-
My recommendation would be to evaluate the `guess` variable as a string, do not cast it to int just yet. Based on the input, if it's not "exit" or "restart" then continue your evaluation. – Pete -S- Aug 07 '20 at 18:27
-
You may find an example of a [similar](https://stackoverflow.com/a/63041611/5045688) program interesting. – Alexander Petrov Aug 07 '20 at 19:09
-
@Ryan, can you check my answer? If my answer solved your problem, please click '✔' to mark the appropriate reply as the answer. If not, please feel free to let me know. – Jack J Jun Aug 12 '20 at 02:36
2 Answers
0
you can use "sentinel-controlled C# while loop"!
you could learn more by visiting :- https://kodify.net/csharp/loop/sentinel-loop/
or you could try creating a method that contains if statement that checks if the user types for example "exit" and then you have to put that method name in every time the user could type and it'll stay rolling in the while which is good , hope i helped you !
thumps up !

Ghaith E Alnaimat
- 49
- 9
0
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:

Jack J Jun
- 5,633
- 1
- 9
- 27