简体   繁体   中英

How to add Exit or Retry in C# Coding (Visual Studio)

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.

在此处输入图像描述

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 !

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:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM