我正在尝试用C#创建一个基本的损坏计算器,但有些东西不能正常工作

时间:2013-11-25 20:12:11

标签: c#

我正在尝试创建一个非常基本的损坏计算器,但由于某种原因,当我运行程序并设置值时,它出现为0.这是我的代码:

在AbilityDamage.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1.Files
{
    class AbilityDamage
    {
        static void Main()
        {
            Console.WriteLine("Enter ability name:");
            string abilityName = Console.ReadLine();

            Ability RocketGrab = new Ability();
            Ability MegaInfernoBomb = new Ability();
            Ability Cataclysm = new Ability();
            Ability MysticShot = new Ability();
            Ability SuperMegaDeathRocket = new Ability();
            Ability Hemoplague = new Ability();
            Ability SprayAndPray = new Ability();
            Ability BusterShot = new Ability();
            Ability AssaultAndBattery = new Ability();
            Ability ExplosiveCask = new Ability();
            Ability PiercingArrow = new Ability();
            Ability Sear = new Ability();

            if (abilityName == "Rocket Grab")
            {
                RocketGrab.AbilityDamageCalculator();
            }
            else if (abilityName == "Mega Inferno Bomb")
            {
                MegaInfernoBomb.AbilityDamageCalculator();
            }
            else if (abilityName == "Cataclysm")
            {
                Cataclysm.AbilityDamageCalculator();
            }
            else if (abilityName == "Mystic Shot")
            {
                MysticShot.AbilityDamageCalculator();
            }
            else if (abilityName == "Super Mega Death Rocket")
            {
                SuperMegaDeathRocket.AbilityDamageCalculator();
            }
            else if (abilityName == "Hemoplague")
            {
                Hemoplague.AbilityDamageCalculator();
            }
            else if (abilityName == "Spray and Pray")
            {
                SprayAndPray.AbilityDamageCalculator();
            }
            else if (abilityName == "Buster Shot")
            {
                BusterShot.AbilityDamageCalculator();
            }
            else if (abilityName == "Assault and Battery")
            {
                AssaultAndBattery.AbilityDamageCalculator();
            }
            else if (abilityName == "Explosive Cask")
            {
                ExplosiveCask.AbilityDamageCalculator();
            }
            else if (abilityName == "Piercing Arrow")
            {
                PiercingArrow.AbilityDamageCalculator();
            }
            else if (abilityName == "Sear")
            {
                Sear.AbilityDamageCalculator();
            }
            else
            {
                Console.WriteLine("Ability not recognized.");
            }

            RocketGrab.name = "Rocket Grab";
            RocketGrab.baseDamage = 80;
            RocketGrab.apRatio = 1;
            MegaInfernoBomb.name = "Mega Inferno Bomb";
            MegaInfernoBomb.baseDamage = 250;
            MegaInfernoBomb.apRatio = 0.9;
            Cataclysm.name = "Cataclysm";
            Cataclysm.baseDamage = 200;
            Cataclysm.adRatio = 1.5;
            MysticShot.name = "Mystic Shot";
            MysticShot.baseDamage = 35;
            MysticShot.adRatio = 1;
            MysticShot.apRatio = 0.2;
            SuperMegaDeathRocket.name = "Super Mega Death Rocket";
            SuperMegaDeathRocket.baseDamage = 125;
            SuperMegaDeathRocket.adRatio = 0.5;
            Hemoplague.name = "Hemoplague";
            Hemoplague.baseDamage = 150;
            Hemoplague.apRatio = 0.7;
            SprayAndPray.name = "Spray and Pray";
            SprayAndPray.baseDamage = 20;
            BusterShot.name = "Buster Shot";
            BusterShot.baseDamage = 300;
            BusterShot.apRatio = 1.5;
            AssaultAndBattery.name = "Assault and Battery";
            AssaultAndBattery.baseDamage = 200;
            AssaultAndBattery.adRatio = 1.4;
            ExplosiveCask.name = "Explosive Cask";
            ExplosiveCask.baseDamage = 200;
            ExplosiveCask.apRatio = 1;
            PiercingArrow.name = "Piercing Arrow";
            PiercingArrow.baseDamage = 10;
            PiercingArrow.adRatio = 1;
            Sear.name = "Sear";
            Sear.baseDamage = 80;
            Sear.apRatio = 0.65;
        }
    }
}

在CalculateDamage.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1.Files
{
    class Ability
    {
        public double baseDamage;
        public double apRatio;
        public double adRatio;
        public string name;

        public void AbilityDamageCalculator()
        {
            Console.WriteLine("Enter your ap value:");
            double ap = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Enter your ad value:");
            double ad = Convert.ToDouble(Console.ReadLine());
            double bonusDamage = apRatio * ap + adRatio * ad;
            double totalDamage = baseDamage + bonusDamage;
            Console.WriteLine(name + "deals " + totalDamage + " damage at level 1.");
        }
    }
}

非常感谢任何有关此问题的帮助!

3 个答案:

答案 0 :(得分:4)

所有值都在main方法中设置。

当你拿到火箭时,它会执行

RocketGrab.AbilityDamageCalculator();

但在此方法中没有值。这两个都是0,因此在乘以时返回0。

public double apRatio;
public double adRatio;

如果要使其起作用,则必须创建适当的对象并设置比率的值。更好的方法是重新思考你的概念并开始创建在其中硬编码这些值的子类(或者想到像数据库这样的数据源)。

当您实际为某项异能设定基本值和其他值时,您已经完成了计算。

答案 1 :(得分:3)

在您要求用户输入并执行计算之前,您似乎没有初始化您的能力值。

答案 2 :(得分:2)

  public double baseDamage;
  public double apRatio;
  public double adRatio;
  public string name;

您永远不会为此变量设置值。因此

  double bonusDamage = apRatio * ap + adRatio * ad;
  double totalDamage = baseDamage + bonusDamage;

等于

 double bonusDamage = 0 * ap +0* ad;
 double totalDamage = 0 + bonusDamage;

这将是0。