我的控制台应用程序“游戏”中存在问题

因此,我在c#控制台应用程序项目中制作了一个“游戏”,该项目基本上是一个必须杀死怪物的英雄,并且英雄和怪物都具有HP以及随机给予和承受的伤害(应该如此)。 我有几个小问题,这些问题对我来说毫无意义。我在游戏中添加了一个再生药水,显然可以为玩家增加10-30之间的随机生命值。

//Regenerating or not
        if (isRegen == false) //if it doesn't
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("You've failed to regenerate!");
            Console.WriteLine("Enemy: " + monster.name);
            Console.WriteLine("Enemy's hp: " + monster.hp);
            GetDamage();
        }
        else //if it does
        {
            if (myHP + regenAmount > 100 || myHP == 100)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("You can't regenerate above 100 hp.");
                Game();
            }
            else
            {
                if (potionCounter == 0)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("You are out of health potions. Cannot regenerate!");
                    Game();
                }
                potionCounter--;
                myHP += regenAmount;
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("You consumed a Health Potion!");
                Console.WriteLine("Your hp was regenerated successfully! HP is raised by " + regenAmount);
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Enemy: " + monster.name);
                Game();
            }
        }

Now, as you can see in the code above, I have made an if statement that checks whether the sum of myHP and the regenAmount is higher than 100, a message that says "You can't regenerate above 100 hp" is displayed. Thing is, when I try this, sometimes it does display the message as presented here, but eventually it decides to display "You've failed to regenerate!" in here and well the game just goes on and the player gets hit. (which obviously shouldn't happen). There is even another similar problem with the potionCounter. As presented in the code:

if (potionCounter == 0)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("You are out of health potions. Cannot regenerate!");
                    Game();
                }

每当药水计数器达到0时,它应显示一条消息,提示用户药水不足。它会发生并起作用,但是与先前的问题类似,它有时会忽略if语句,并允许用户修复并继续减小potionCounter变量,或者无法重新生成并被怪物击中。

我知道这些代码有些不好,这是因为这对编程来说是一种新事物,但我会尽我最大的努力发展自己并学习尽可能多的知识,这就是为什么我决定在这里共享它。

感谢您阅读它,希望您找到解决方法:)

评论