ally
ally

Reputation: 15

How to call main

I've only been learning C# for a couple days and I was wondering how I would call Main to restart the program when the player says 'yes' during the switch statement (when he is asked to play again)

    public static void Main(string[] args)
    {
        Console.WriteLine("Choose a gun to shoot at Toaster... ");
        Console.Write("rocket/sniper/rifle/pistol: ");
        string playersGunChoice = Console.ReadLine();
        Random randomweapondmg = new Random();
        int weapondmg = 0;
        switch (playersGunChoice.ToLower())
   {
        case "rocket":
                Console.WriteLine("You chose a rocket.");
                weapondmg = randomweapondmg.Next(75, 200);
                Console.WriteLine("Your rocket does " + weapondmg + " to Toaster.");
                break;
        case "sniper":
                Console.WriteLine("You chose a sniper.");
                weapondmg = randomweapondmg.Next(50, 150);
                Console.WriteLine("Your sniper does " + weapondmg + " to Toaster.");
                break;  
   }

        int ToasterHealth = 500;
        int ToastersLeftHp = ToasterHealth - weapondmg;
        Console.WriteLine("Toaster has " + ToastersLeftHp + " healthpoints left.");
        if (ToastersLeftHp != 0)
            Console.WriteLine("Shoot at Toaster again?");
        Console.Write("yes/no: ");
        string PlayAgain = Console.ReadLine();
            switch(PlayAgain.ToLower())
            {
                case "yes": //I want to call main here somehow
                    break;
                case "no":
                    break;
                default:
                    Console.WriteLine("That wasn't a yes or no.");
                    break;
         }
            if (ToastersLeftHp == 0)
                Console.WriteLine("You killed Toaster!");
            else if (ToastersLeftHp < 100)
                Console.WriteLine("Toaster is almost dead! He has " + ToastersLeftHp + " healthpoints left.");
    }
    }
}

Upvotes: 2

Views: 2133

Answers (2)

Adi
Adi

Reputation: 2094

As a guideline you should try some online tutorials to help you write proper code. Try avoiding calling the main method as it is the starting point for your program, instead use a different function or even better a different class to represent the game. this function\class can call it self or add an inner loop that runs until the game is 'done'. Also consider dividing the code into smaller functions, it would be more maintainable and readable.

Upvotes: 0

Servy
Servy

Reputation: 203842

You call it the same way you call any other method. You write the name and pass it the arguments it expects:

Main(string[]{});

If you don't want the program to continue doing what it was doing after it finishes calling Main, you'd want to make sure that it stops executing gracefully after that point.

Having said all of that, making Main recursive isn't exactly a solution to that problem that I would advise. I'd strongly suggest simply applying a loop in your main method that continually performs the logic that you have until you want it to stop, and have each iteration of the loop finish when you either need to restart, or are completely done.

Upvotes: 4

Related Questions