我是编码的新手,我正在尝试根据他们的性别,身高,体重和运动量来计算一个人基础代谢率。应用程序应该获取所提供的信息并根据其性别运行一个等式来计算它们的 BMR 。接下来,它应该取人的 BMR 并乘以这取决于该人锻炼的频率。到目前为止,我已经能够使应用程序基于性别运行不同的等式。但是,我无法让应用程序从第一个等式中得到答案,并将其乘以行使的数量。如果我在描述中不够具体,我会道歉。我对编码很陌生,真的不知道我在做什么。
感谢任何和所有帮助。这是我的代码:
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
{
public calorieCalculator()
{
InitializeComponent();
}
private void calorieCalculator_Load(object sender, EventArgs e)
{
}
private void calculateButton_Click(object sender, EventArgs e)
{
string gender, exercise;
double height, weight, age, malebmr, femalebmr; //variables
decimal bmrmult;
// 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 ;
bmrDisplay.Text = ("Your base metabolic rate burns " + malebmr + " calories");
break;
case "Female":
femalebmr = weight * 10 + height * 6.25 - age * 5 - 161;
MessageBox.Show("You should eat:" + femalebmr + "calories");
break;
}
}
}
private void caloriesButton_Click(object sender, EventArgs e)
{
if (exerciseList.SelectedIndex != -1)
{
string exercise;
decimal bmrmult;
exercise = exerciseList.SelectedItem.ToString();
switch (exercise)
{
case "Light exercise (1–3 days per week)":
bmrmult = 1.375M;
break;
case "Moderate exercise (3–5 days per week)":
bmrmult = 1.55M;
break;
case "Heavy exercise (6–7 days per week)":
bmrmult = 1.725M;
break;
case "Very heavy exercise (twice per day, extra heavy workouts)":
bmrmult = 1.9M;
break;
}
}
}
}
}
编辑:我试图修改我的代码并取消我的第二个按钮。但是,现在我遇到了一个全新的问题。我的 BMR 未乘以所需的bmrmult
。事实上,当我点击按钮的一半时间我得到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 = (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;
}
}
}
答案 0 :(得分:0)
我相信"来自第一个等式的回答"这个人的BMR对吗?
解决此问题的一种快捷方法是更改malebmr
和femalebmr
变量的范围。
在double malebmr, femalebmr;
函数之外声明calculateButton_Click()
应该允许您在caloriesButton_Click()
中使用它们。