0

So I'm a fresh beginner and I made this small game where the enemy dies at 0hp, but my program keeps closing after the first time I use the damage ability

 static void Main(string[] args)
        {
            string hp = "";
            int damage;
            int level;
            int experience = 0;
            int kills = 0;
            int enemy1hp = 200;


            level = 1 + experience;
            experience = kills * 1;
            damage = 50 * level;
            enemy1hp = 200;

            Console.WriteLine("Welcome!");
            hp = "Enemy 1 currently has " + enemy1hp.ToString() + "hp!";
            Console.WriteLine(hp);

            string userValue = Console.ReadLine();
            if (userValue == "1")
            {
                Console.WriteLine("You have used Haven Strike!");
                enemy1hp = enemy1hp - damage;
                hp = "Enemy 1 currently has " + enemy1hp.ToString() + "hp!";
            }

            Console.WriteLine(hp);
            Console.ReadLine();

            if (enemy1hp <= 0)
            {
                kills = kills + 1;
            }
        }

Any ideas what I can change?

Ayy Lmao
  • 3
  • 3

1 Answers1

3

Use while loop for this

static void Main(string[] args)
{
    string hp = "";
    int damage;
    int level;
    int experience = 0;
    int kills = 0;
    int enemy1hp = 200;

    while (enemy1hp > 0)
    {
        level = 1 + experience;
        experience = kills * 1;
        damage = 50 * level;

        Console.WriteLine("Welcome!");
        hp = "Enemy 1 currently has " + enemy1hp.ToString() + "hp!";
        Console.WriteLine(hp);

        string userValue = Console.ReadLine();
        if (userValue.equals("1"))
        {
            Console.WriteLine("You have used Haven Strike!");
            enemy1hp = enemy1hp - damage;
            hp = "Enemy 1 currently has " + enemy1hp.ToString() + "hp!";
        }

        Console.WriteLine(hp);
        Console.ReadLine();

        if (enemy1hp <= 0)
        {
            kills = kills + 1;
        }
    }
}

This will run as long as enemy1hp is bigger than 0. You should also remove the enemy1hp = 200; assignment.

Guy
  • 46,488
  • 10
  • 44
  • 88
  • 1
    And most important, don't do this in the main. Read about classes properties and methods. – Guillaume Beauvois Apr 04 '16 at 09:08
  • Are you sure you'd want to loop from the start of the program? Resetting the variables each time? – Oliver Nicholls Apr 04 '16 at 09:08
  • Even if the code works, it's not a good answer. Do everything in the main ? If he's a beginner you should teach him good practice NOW. It's like saying to someone whining about how that life is difficult : kill yourself. Simplest answer ever but not the good one. – Guillaume Beauvois Apr 04 '16 at 09:14
  • 1
    I disagree with @GuillaumeBeauvois, ofcourse it could be solved with more structure like using methods/functions/classes, but as he mentioned he is a beginner. My advise is give a solution at level. I think this will meet the question. _I won't build/buy a truck if someone requested a car for shopping._ – Jeroen van Langen Apr 04 '16 at 09:24