Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我如何修正我的损失计算如此之高?_C#_Math_Console Application - Fatal编程技术网

C# 我如何修正我的损失计算如此之高?

C# 我如何修正我的损失计算如此之高?,c#,math,console-application,C#,Math,Console Application,我已经为命令行RPG编写了一些C代码。问题是:无论何时我击中敌人,它都会受到太高的伤害 我已经为损坏创建了一个int变量,并且只调用了一次DamageCalc。问题仍然存在。 我假设这可能是我在第154行从敌人的生命中减去伤害的方式 下面是重现问题的代码。请原谅我写得太长了,但我不知道问题到底发生在哪里,所以我不能把它写得比我已经做的还要短 在这里你可以看到野猪有5点生命。我击中野猪,造成4点伤害。可悲的是,野猪受到10点伤害,生命值下降到-5 using System; namespace

我已经为命令行RPG编写了一些C代码。问题是:无论何时我击中敌人,它都会受到太高的伤害

我已经为损坏创建了一个int变量,并且只调用了一次DamageCalc。问题仍然存在。 我假设这可能是我在第154行从敌人的生命中减去伤害的方式

下面是重现问题的代码。请原谅我写得太长了,但我不知道问题到底发生在哪里,所以我不能把它写得比我已经做的还要短

在这里你可以看到野猪有5点生命。我击中野猪,造成4点伤害。可悲的是,野猪受到10点伤害,生命值下降到-5

using System;

namespace ConsoleApp2
{
    class Player
    {
        Archer archer = new Archer();
        Knight knight = new Knight();
        Assasin assasin = new Assasin();
        CurrentWeapon currentWeapon = new CurrentWeapon();
        public string Name { get; set; }
        public string Klasse { get; set; }
        public int HP { get; set; } = 20;
        public int Level { get; set; } = 0;
    }

    class CurrentWeapon
    {
        public string WeaponName { get; set; }
        public float DamageMultiplier { get; set; } = 1;
    }

    class Enemy
    {
        public string Type { get; set; }
        public int Attack { get; set; }
        public int HP { get; set; }
        public int Experience { get; set; }
        public bool Dead { get; set; } = false;

        //Konstruktor
        public Enemy(string _type, int _attack, int _hp, int _experience)
        {
            Type = _type;
            Attack = _attack;
            HP = _hp;
            Experience = _experience;
        }
    }

    class Archer
    {
        public string ClassName { get; set; } = "Archer";
        public int BaseDamage { get; set; } = 6;
        public string WeaponType { get; set; } = "Bow";
    }
    class Knight
    {
        public string ClassName { get; set; } = "Knight";
        public int BaseDamage { get; set; } = 5;
        public string WeaponType { get; set; } = "Greatsword";
    }
    class Assasin
    {
        public string ClassName { get; set; } = "Assasin";
        public int BaseDamage { get; set; } = 4;
        public string WeaponType { get; set; } = "Dagger";
    }

    class Program
    {
        static int GenerateNumber(int min, int max)
        {
            Random random = new Random();
            return random.Next(min, max);
        }

        static void Main(string[] args)
        {
            Archer archer = new Archer();
            Knight knight = new Knight();
            Assasin assasin = new Assasin();
            CurrentWeapon currentWeapon = new CurrentWeapon();
            Player player = new Player();

            int DamageCalc()
            {
                if (player.Klasse == "Archer")
                {
                    float outputDamage = archer.BaseDamage * currentWeapon.DamageMultiplier;
                    return (int)Math.Round(outputDamage);
                }
                if (player.Klasse == "Knight")
                {
                    float outputDamage = knight.BaseDamage * currentWeapon.DamageMultiplier;
                    return (int)Math.Round(outputDamage);
                }
                if (player.Klasse == "Assasin")
                {
                    float outputDamage = assasin.BaseDamage * currentWeapon.DamageMultiplier;
                    return (int)Math.Round(outputDamage);
                }
                else { return 0; }
            }
            Console.WriteLine(" What shall you be called?");
            player.Name = Console.ReadLine();
            Console.ReadKey();
            Console.WriteLine(player.Name);
            Console.Clear();
            Console.WriteLine("Good, " + player.Name + " choose your class.");
            Console.WriteLine();
            Console.WriteLine(archer.ClassName + " | DMG: " + archer.BaseDamage + " | Weapon Type: " + archer.WeaponType);
            Console.WriteLine(knight.ClassName + " | DMG: " + knight.BaseDamage + " | Weapon Type: " + knight.WeaponType);
            Console.WriteLine(assasin.ClassName + " | DMG: " + assasin.BaseDamage + " | WeaponType : " + assasin.WeaponType);

            string UserClass = Console.ReadLine();
            if (UserClass == "Archer")
            {
                player.Klasse = "Archer";
                Console.WriteLine("You are now an Archer.");
                currentWeapon.WeaponName = "Oak bow";
                Console.WriteLine("You got a " + currentWeapon.WeaponName + ".");
            }

            if (UserClass == "Knight")
            {
                player.Klasse = "Knight";
                Console.WriteLine("You are now a Knight.");
                currentWeapon.WeaponName = "Wooden sword";
                Console.WriteLine("You got a " + currentWeapon.WeaponName + ".");
            }

            if (UserClass == "Assasin")
            {
                player.Klasse = "Assasin";
                Console.WriteLine("You are now an Assasin.");
                currentWeapon.WeaponName = "Wooden Dagger";
                Console.WriteLine("You got a " + currentWeapon.WeaponName + ".");
            }
            Console.ReadKey();

            bool Play = true;
            while (Play)
            {
                Console.Clear();
                Console.WriteLine("Press any Key to walk.");
                Console.ReadKey();
                Console.Clear();

                int randomNumber = Program.GenerateNumber(0, 101);

                if (randomNumber <= 101 && randomNumber >= 0)
                {
                    Enemy enemy1 = new Enemy("Wild Boar", Program.GenerateNumber(5, 10), Program.GenerateNumber(2, 4), Program.GenerateNumber(6, 9));
                    Console.WriteLine(" A " + enemy1.Type + " appears! | ATTK: " + enemy1.HP + " | HP: " + enemy1.Attack);
                    while (!enemy1.Dead)
                    {
                        Console.WriteLine("Use your " + currentWeapon.WeaponName + " and attack by pressing 'Q'.");

                        if (Console.ReadKey(true).Key == ConsoleKey.Q)
                        {
                        int hitDamage = DamageCalc();
                        enemy1.HP -= hitDamage;
                        Console.WriteLine("You hit the " + enemy1.Type + "! | HP: " + enemy1.HP + " (You did -" + hitDamage +")");
                        if (enemy1.HP <= 0)
                        {
                            Console.WriteLine("You killed the " + enemy1.Type + "! | +" + enemy1.Experience + " Experience!");
                            enemy1.Dead = true;
                        }
                            Console.ReadKey();
                        }
                    }
                }
            }
        }
    }
}
第一个错误:

Console.WriteLine(" A " + enemy1.Type + " appears! | ATTK: " + enemy1.HP + " | HP: " + enemy1.Attack);
您在攻击时打印HP,反之亦然

第二个错误:

enemy1.HP -= DamageCalc();
Console.WriteLine("You hit the " + enemy1.Type + "! | HP: " + (enemy1.HP - DamageCalc()) + " (You did -" + DamageCalc() + ")");

enemy1.HP已经减少-=伤害,所以打印时不要再减去HP:+enemy1.HP-DamageCalc

您可以对输出使用字符串插值,因为它更容易读取

Console.WriteLine$A{enemy1.Type}出现!攻击:{enemy1.ATTACK}HP{enemy1.HP}


你可能会考虑增加随机数,因为你的HP数在2到3之间,这就是为什么你可以一次命中它们= d< /p>试着为你的计算器写一些测试。你多次降低HP——在输出任何东西之前一次,然后再显示你损坏了它们。将Console.WriteLine更改为只包含enemy1.HP而不是enemy1.HP-DamageCalcI尚未尝试任何操作,因为对我来说,代码是100%有意义的,但您已经知道,您的代码不知何故在某些地方不是100%有意义的。建议您使用调试器来了解代码的实际功能,以及在程序执行过程中,它的状态和每个相关变量的值是如何随每个步骤而变化的。如果你还不知道调试器,这里有一些方便的指南:,@KevinGarnick,是的,这个游戏在法语中被称为Conso,选择的名称空间反映了:Conso,le App 2…我还没有尝试过任何东西,因为对我来说,代码是100%合理的-这是你最大的问题。学习如何编写好的单元测试。谢谢。已经修复了第二个错误,但没有首先注意到。作品now@Varvalian您可以对输出使用字符串解释,例如:Console.WriteLine$A{enemy1.Type}出现!攻击:{enemy1.ATTACK}HP{enemy1.HP};

var damage = enemy1.HP -= DamageCalc();
Console.WriteLine($"You hit the {enemy1.Type} HP: {damage}  You did {DamageCalc()} damage") ;