【问题标题】:c# Daily Calorie Calculator. How do I multiply a user input by another variable?c#每日卡路里计算器。如何将用户输入乘以另一个变量?
【发布时间】:2017-11-13 10:44:23
【问题描述】:

我对编码很陌生,因此对于任何业余错误,我深表歉意。我正在尝试编写一个问题,该问题可以根据他们的性别、身高、体重、年龄和运动量来计算人的基础代谢率。在我尝试这样做时,我尝试使用两个 if 语句。第一个 if 语句表示,如果他们选择男性/女性,它将通过某种算法运行用户输入。第二个 if 语句应该是用户锻炼的频率。第二个输入是我遇到困难的输入。我不知道如何将 BMR 乘以锻炼量。很多时候,答案是 0。我真的开始认为我的逻辑搞砸了。我会接受任何和所有的批评、建议和建议。我只是想学习!

谢谢。

这是我的代码

    namespace WindowsFormsApplication9
{
    public partial class calorieCalculator : Form
    {
        double malebmr, femalebmr, calories;
        int bmrmult;
        string gender, exercise;
        double height, weight, age;//variables

        public calorieCalculator()
        {
            InitializeComponent();
        }

        private void calorieCalculator_Load(object sender, EventArgs e)
        {



        }

        private void calculateButton_Click(object sender, EventArgs e)
        {



            //get personal information
            height = double.Parse(heightTextBox.Text);
            weight = double.Parse(weightTextBox.Text);
            age = double.Parse(ageTextBox.Text);
            //select gender
           if (genderList.SelectedIndex != -1)
            {
                gender = genderList.SelectedItem.ToString();
                switch (gender)
                {
                    case "Male":
                        //perform calculation
                        malebmr = (weight * 10 + height * 6.25 - age * 5 - 5);
                        calories = malebmr * bmrmult;
                        bmrDisplay.Text = ("Your base metabolic rate burns " + calories+ " calories");
                        break;
                    case "Female":
                        femalebmr = weight * 10 + height * 6.25 - age * 5 - 161;
                        MessageBox.Show("You should eat:" + femalebmr + "calories");
                        break;
                }
            }
           if (exerciseList.SelectedIndex !=-1)
            {
                string exercise;
                exercise = exerciseList.SelectedItem.ToString();
                switch (exercise)
                {
                                    case "Light exercise (1–3 days per week)":
                                       bmrmult = (decimal)1.375;
                               break;
                                      case "Moderate exercise (3–5 days per week)":
                                         bmrmult = (int)1.55m;
                                        break;
                                    case "Heavy exercise (6–7 days per week)":
                                        bmrmult = (int)1.725m;
                                        break;
                                     case "Very heavy exercise (twice per day, extra heavy workouts)":
                                         bmrmult = (int)1.9m;
                                        break;
                }
            }
        }

编辑:这里是公式

if Male
BMR = (10 × weight in kg) + (6.25 × height in cm) - (5 × age in years) + 5

 if Female
BMR = (10 × weight in kg) + (6.25 × height in cm) - (5 × age in years) - 161


Little to no exercise   Daily kilocalories needed = BMR x 1.2
Light exercise (1–3 days per week)  Daily kilocalories needed = BMR x 1.375
Moderate exercise (3–5 days per week)   Daily kilocalories needed = BMR x 1.55
Heavy exercise (6–7 days per week)  Daily kilocalories needed = BMR x 1.725
Very heavy exercise (twice per day, extra heavy workouts)   Daily kilocalories needed = BMR x 1.9

编辑 2:这是我的更新代码!

 `using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication9
{
    public partial class calorieCalculator : Form
    {
        double malebmr, femalebmr, calories;
        int bmrmult;
        string gender, exercise;
        double height, weight, age;//variables

        public calorieCalculator()
        {
            InitializeComponent();
        }

        private void calorieCalculator_Load(object sender, EventArgs e)
        {



        }

        private void calculateButton_Click(object sender, EventArgs e)
        {



            //get personal information
            height = double.Parse(heightTextBox.Text);
            weight = double.Parse(weightTextBox.Text);
            age = double.Parse(ageTextBox.Text);
            //select gender
            if (exerciseList.SelectedIndex != -1)
            {
                string exercise;
                exercise = exerciseList.SelectedItem.ToString();
                switch (exercise)
                {
                    case "Light exercise (1–3 days per week)":
                        bmrmult = (int)1.375m;
                        break;
                    case "Moderate exercise (3–5 days per week)":
                        bmrmult = (int)1.55m;
                        break;
                    case "Heavy exercise (6–7 days per week)":
                        bmrmult = (int)1.725m;
                        break;
                    case "Very heavy exercise (twice per day, extra heavy workouts)":
                        bmrmult = (int)1.9m;
                        break;
                }
            }
            if (genderList.SelectedIndex != -1)
            {
                gender = genderList.SelectedItem.ToString();
                switch (gender)
                {
                    case "Male":
                        //perform calculation
                        malebmr = (weight * 10) + (height * 6.25) - (age * 5) - 5;
                        calories = malebmr * bmrmult;
                        bmrDisplay.Text = ("Your base metabolic rate burns " + calories + " calories");
                        break;
                    case "Female":
                        femalebmr = weight * 10 + height * 6.25 - age * 5 - 161;
                        MessageBox.Show("You should eat:" + femalebmr + "calories");
                        break;
                }
            }
        }
    }
}

【问题讨论】:

  • 你调试代码了吗?您是否检查了哪一行代码会导致问题?一般测量BMR的公式是什么?看看你做计算的方式,我认为你需要纠正它。
  • bmrmult 相关代码最后执行,这就是为什么当你根据性别计算 BMR 时它的值将为零的原因。这就是为什么卡路里值一直为零的原因calories = malebmr * bmrmult 为零,因为 bmrmult 为零。
  • @ChetanRanpariya 好主意。不幸的是,我切换了代码,但仍然没有得到正确的计算。这是我的公式。
  • @ChetanRanpariya 我用公式更新了帖子。
  • 尝试将括号与代码中的公式相同。 malebmr = (weight * 10) + (height * 6.25) - (age * 5) - 5

标签: c#


【解决方案1】:

在将值放入bmrmult 之前将它们转换为整数时,您将消除该值的任何非整数部分。所以bmrmult 在所有情况下都将变为 1。您应该将bmrmult 的变量类型更改为decimal 或可以保存十进制值的变量类型,并删除(int) 强制转换。

我还建议基于exerciseList.SelectedIndex 而不是exerciseList.SelectedItem.ToString() 进行切换,因为您的字符串比您的元素序列更有可能发生变化,而且全文中的错字比在列表中的索引。

【讨论】:

  • 现在我收到错误 CS0019 Operator '*' cannot be applied to 'double' and 'decimal' 类型的操作数
  • 您有 3 个选择: 1) 将 bmrmult 更改为 double 而不是 decimal; 2)将程序中的每个“双精度”更改为“十进制”; 3) 在您的计算中应用显式强制转换,例如 calories = malebmr * (double)bmrmult
  • 另见stackoverflow.com/questions/8903632/…stackoverflow.com/questions/1165761/…。我相信你不能将它们相乘的原因是因为它们处理不同的权衡,当你乘以一个时,编译器无法猜测你在结果值中需要哪一组权衡。
猜你喜欢
  • 2010-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多