我对编码很陌生,所以我为任何业余错误道歉。我试图根据性别,身高,体重,年龄和锻炼的程度来计算可以计算人基础代谢率的问题。在我尝试这样做时,我尝试使用两个if语句。第一个if语句说如果他们选择Male / Female,它将通过某种算法运行用户输入。第二个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;
}
}
}
}
}
答案 0 :(得分:0)
在将值转换为bmrmult
之前将值转换为整数时,您将消除该值的任何非整数部分。所以bmrmult
在所有情况下都会变为1。您应该将bmrmult
的变量类型更改为decimal
或可以包含小数值的内容,并删除(int)
强制转换。
我还建议根据exerciseList.SelectedIndex
而不是exerciseList.SelectedItem.ToString()
进行切换,因为您的字符串更可能会比您的元素序列更改,并且更有可能会出现完整错误文本比列表中的索引。