除以零例外,知道为什么会抛出,不知道如何解决

罗伯特·纽托瓦

为了更好地理解,我将首先发布我的程序类。

程序.cs

namespace BeginnersGuidToLifting
{
    class Program
    {
        public static readonly int width = 85;
        public static readonly int height = 35;


        public static readonly User user = new User();
        public static readonly Goal goal = new Goal();
        public static Calories calories = new Calories();
        public static Nutrition nutrition = new Nutrition();
        public static Menu menu = new Menu();

        static void InitUser()
        {
            Console.WriteLine("\n### About You ###\n");

            user.Name = Question.AskString("Name: ");
            user.Age = Question.AskInt("Age: ");
            user.Gender = Question.AskString("Gender(m/f): ");
            user.Height = Question.AskInt("Height(cm): ");
            user.Weight = Question.AskInt("Weight(kg): ");

        }

        static void InitGoals()
        {
            Console.WriteLine("\n### What are your Goals ### \n");

            goal.GainWeight();
            goal.LoseWeight();
            goal.GetStronger();
            goal.GetLeaner();
            goal.StayFit();
            goal.Hypertrophy();

            Console.Write("Answer: ");
            var input = Console.ReadKey();

            switch (input.Key)
            {
                case ConsoleKey.D1:
                    goal.GoalStatusGainWeight = true;
                    goal.GoalMessage = "Your Goal is to Gain Weight";
                    break;
                case ConsoleKey.D2:
                    goal.GoalStatusLoseWeight = true;
                    goal.GoalMessage = "Your Goal is to Lose weight";
                    break;
                case ConsoleKey.D3:
                    goal.GoalStatusGetStronger = true;
                    goal.GoalMessage = "Your Goal is to get Stronger";
                    break;
                case ConsoleKey.D4:
                    goal.GoalStatusGetLeaner = true;
                    goal.GoalMessage = "Your Goal is to get Leaner";
                    break;
                case ConsoleKey.D5:
                    goal.GoalStatusStayFit = true;
                    goal.GoalMessage = "Your Goal is to Stay Fit";
                    break;
                case ConsoleKey.D6:
                    goal.GoalStatusHypertrophy = true;
                    goal.GoalMessage = "Your Goal is Hypertrophy";
                    break;
                default:
                    Console.WriteLine("Error");
                    break;
            }
        }

        static void InitMenu()
        {
            menu.ShowMenu();
            menu.Discalmer = false;
            //menu.Disclamer();

        }

        static void BmiResult()
        {
            var bodyType = user.GetBodyType();

            if (bodyType == User.BodyType.ReallyUnderweight)
            {
                Console.WriteLine("Really Underweigt");
            }
            else if (bodyType == User.BodyType.Underweight)
            {
                Console.WriteLine("Underweight");
            }
            else if (bodyType == User.BodyType.CloseToUnderwight)
            {
                Console.WriteLine("Close to Underweight");
            }
            else if (bodyType == User.BodyType.Healthy)
            {
                Console.WriteLine("at a Healthy Weight");
            }
            else if (bodyType == User.BodyType.Overweight)
            {
                Console.WriteLine("Overweight");
            }
            else if (bodyType == User.BodyType.Obese)
            {
                Console.WriteLine("Obese");
            }
            else if (bodyType == User.BodyType.SeverlyObese)
            {
                Console.WriteLine("Serverley Obese");
            }
            else if (bodyType == User.BodyType.MorbidlyObese)
            {
                Console.WriteLine("Morbidly Obese");
            }
        }

        static void Main(string[] args)
        {


            Console.Title = "Beginers Guide To Lifting";
            Console.SetWindowSize(width, height);
            Console.SetBufferSize(width, height);


            InitMenu();

            var userInput = Console.ReadKey();
            switch (userInput.Key)
            {
                case ConsoleKey.D1:
                    InitUser();
                    InitGoals();
                    break;
                case ConsoleKey.D2:
                    Console.WriteLine("Press 2");
                    break;
                default:
                    Console.WriteLine("Why y no work");
                    break;
            }


            //Status So Far
            Console.WriteLine("\n### Your Stats ### \n");

            Console.WriteLine("Name: {0} \nAge: {1} \nHeight: {2} \nWeight: {3}", user.Name, user.Age, user.Height, user.Weight);
            Console.WriteLine("Goal: {0}", goal.GoalMessage);

            BmiResult();


            Console.Write("\nContinue(y/n) to The Programm?: ");
            if (Console.ReadLine() == "y")
            {
                Console.Clear();
            }

            //Gain Weight Goal
            //Introduce 5x5
            var exercise = new FiveByFive();
            exercise.printIntroduction();

            //Show Exercises
            exercise.printWorkoutWeek1();
            exercise.printWorkoutWeek2();

            //Show Nutrition
            if (goal.GoalStatusGainWeight == true)
            {
                switch (user.Gender)
                {
                    case "male":
                    case "m":
                    case "Male":
                        calories.GainCaloriesMale(user);
                        break;
                    case "female":
                    case "f":
                    case "Female":
                        calories.GainCaloriesFemale(user);
                        break;
                }
            }

            //Lose Weight
            if (goal.GoalStatusLoseWeight == true)
            {
                switch (user.Gender)
                {
                    case "male":
                    case "m":
                    case "Male":
                        calories.LoseCaloriesMale(user);
                        break;
                    case "female":
                    case "f":
                    case "Female":
                        calories.LoseCaloriesFemale(user);
                        break;
                }
            }
        }
    }
}

问题出在我的主要方法中。

        InitMenu();

        var userInput = Console.ReadKey();
        switch (userInput.Key)
        {
            case ConsoleKey.D1:
                InitUser();
                InitGoals();
                break;
            case ConsoleKey.D2:
                Console.WriteLine("Press 2");
                break;
            default:
                Console.WriteLine("Why y no work");
                break;
        }

当我按下一个时,它应该发生它所显示的。初始化用户和初始化用户目标。当我按下 2 时,它会将我带到 User.cs 类,我正在为用户 BMI 计算数学。

我真的不明白为什么按 2 时会这样做,因为我只想显示“按 2”或稍后添加免责声明。

豁免

System.DivideByZeroException: '试图除以零。'

抛出是因为它试图将 0 与 0 相除,这是因为还没有来自用户的输入。因为我想要通过按 2 来显示另一个带有一些文本的窗口。就是这样。

我真的不明白为什么它会跳转到 User 类。

为了更好地理解,这里是User.cs。

namespace BeginnersGuidToLifting
{
    class User
    {
        public enum BodyType
        {
            ReallyUnderweight,
            Underweight,
            CloseToUnderwight,
            Healthy,
            Overweight,
            Obese,
            SeverlyObese,
            MorbidlyObese
        }

        public string Name;
        //private string _gender;
        public string Gender;
        private int _weight;
        private int _height;
        private int _age;

        public int Age
        {
            get => _age;
            set
            {
                if (value >= 0)
                {
                    _age = value;
                }
            }
        }

        public int Weight
        {
            get => _weight;
            set
            {
                if (value >= 0)
                {
                    _weight = value;
                }
            }
        }

        public int Height
        {
            get => _height;
            set
            {
                if (value >= 0)
                {
                    _height = value;
                }
            }
        }

        //TODO
        //public string Gender
        //{

        //    get => _gender;
        //    set
        //    {
        //        if (value == "female" || value == "f" || value == "male" || value == "m" )
        //        _gender = value;
        //        else
        //        {
        //            Console.WriteLine("Value Wrong");
        //        }
        //    }
        //}


        public int GetBmi()
        {
            Console.WriteLine("Weight:"+Weight);
            //var bmi = Math.Round((Weight / Math.Pow(Height, 2)) * 10000);
            int bmi = Weight / (Height * Height) * 10000;
            Console.WriteLine("Your Body Mass Index: " + bmi);
            Console.Write("That Means That you are: ");
            return bmi;
        }

        public BodyType GetBodyType()
        {
            int bmi = GetBmi();

            if (bmi < 16)
                return BodyType.ReallyUnderweight;
            else if (bmi >= 16 && bmi <= 17)
                return BodyType.Underweight;
            else if (bmi >= 17 && bmi <= 18.5)
                return BodyType.CloseToUnderwight;
            else if (bmi >= 18.5 && bmi <= 25)
                return BodyType.Healthy;
            else if (bmi >= 25 && bmi <= 30)
                return BodyType.Overweight;
            else if (bmi >= 30 && bmi <= 35)
                return BodyType.Obese;
            else if (bmi >= 35 && bmi <= 40)
                return BodyType.SeverlyObese;

            return BodyType.MorbidlyObese;
        }
    }
}

概括

  1. 当在键盘上按下数字 1 和数字 2 时,开关命令调用 2 个动作。
  2. 按 1 时,一切正常,并按其应有的作用。
  3. 当按 2 时,它会尝试对尚未输入的内容进行数学运算,因为仅在按 1 时才要求输入。

预先感谢您抽出时间阅读本文。欢迎任何意见。我希望一切都清楚明白。

ps2山羊

在您按 '2' 并退出 之后switch,它会调用BmiResult(),它会调用用户类中的方法。

BmiResult()call user.GetBodyType(),它调用GetBmi()用户类。这是您的计算发生的地方,但由于您没有为用户设置任何值,所以每件事都默认为零。这意味着你正在这样做:

int bmi = 0 / (0 * 0) * 10000;

操作顺序意味着它这样做:

int bmi = (0 / 0) * 10000;

因为除法和乘法首先在括号内执行,然后从左到右执行。

但是,将零乘以任何东西仍然为零,因此您需要在进行计算之前检查除数是否为零,或者将代码包装在 try/catch 中并处理它。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我的Paint方法运行两次,我不知道为什么。我该如何解决这个问题,有人知道为什么会这样吗?

我的脚本出现语法错误,但我不知道为什么以及如何解决它>

不知道为什么会这样

我的页码显示有错误,我知道为什么以及在哪里但我不知道如何解决它们 (PyQt)

Python:获取“列表索引超出范围”错误;我知道为什么但不知道如何解决这个问题

存在的操作员如何工作?我不知道为什么会这样

如何解决:我的第二个 if 语句出现语法错误,但我不知道为什么?

C ++ unordered_map emplace()函数抛出seg错误,我也不知道为什么

Get 方法抛出错误,不知道为什么

我不知道为什么在这里抛出ArrayIndexOutOfBoundsException

openFileOutput抛出NullPointerException(而且我不知道为什么)

VBA函数仅产生零。不知道为什么

在SQL中创建外键被认为是不兼容的,不知道为什么

不知道为什么Java mergesort算法偶尔会失败

不知道为什么我的程序默认会输出最后一个if语句

试图使这项工作不知道为什么会这样

在执行这个程序时,char 函数会返回变量。我不知道为什么?

我不知道为什么会这样。jedis 创建名为“jedisConnectionFactory”的 bean 时出错

TypeError: 'int' 对象不可迭代,不知道为什么会这样

网络抓取缓慢但不知道为什么

不知道为什么div不会消失?

Action Mailer 不工作,不知道为什么

ArgumentOutOfRangeException,我不知道为什么

é而不是“é”,绝对不知道为什么

我不知道为什么我得到AttributeError

VLOOKUP给#N / A我不知道为什么

不知道为什么我的循环不循环

While 循环随机循环。不知道为什么。

Boost.Asio segfault,不知道为什么