我对 C# 和一般编程还很陌生,但每天都在尝试和学习!
我做了一个应用程序,正在尝试输出用户输入的 BMI,您可以选择英制和公制。
我会在这里粘贴我的代码,但我尝试了多种不同的方法,但似乎只能从我的“bmi”值中得到 0,0。我认为数学应该是正确的,但无论我将什么粘贴到身高和体重中,我总是得到 0,0。
namespace Assignment3
{
public partial class Form1 : Form
{
private string name = "No name";
private BMICalculator2 bmiCalc = new BMICalculator2();
public Form1()
{
InitializeComponent();
InitializeGUI();
}
private void InitializeGUI()
{
this.Text = "The Body Mass Calculator"; // this = me --> Mainform
//input controls
lblHeight.Text = "Height (feet)";
lblWeight.Text = "Weight (lbs)";
//output controls
txtHeight.Text = string.Empty;
txtWeight.Text = string.Empty;
rbtnImperial.Checked = true;
}
private void UpdateHeightText()
{
if (rbtnMetric.Checked)
lblHeight.Text = "Height (cm)";
else
lblHeight.Text = "Height (ft, in)";
if (rbtnImperial.Checked)
lblWeight.Text = "Weight (lbs)";
else
lblWeight.Text = "Weight (kg)";
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void groupBox2_Enter(object sender, EventArgs e)
{
}
private void ReadInputBMI_Click(object sender, EventArgs e)
{
bool ok = ReadInputBMI(); // Calling the method checking BMI imputs
if (ok)
{
CalculateAndDisplayResults();
}
groupBoxResult.Text = "Results for " + name; // changing the text to user's name
}
private void ReadName()
{
// reading & storing the name of user
if (string.IsNullOrEmpty(name))
{
name = "NO NAME";
}
else
{
name = txtName.Text.Trim();
}
}
private bool ReadHeightAndWeight()
{
double height = 0.0;
double weight = 0.0;
double inch = 0.0;
// checking so the height and weight is a valid input
bool ok = double.TryParse(txtHeight.Text, out height);
if (!ok)
{
MessageBox.Show("The height value is invalid", "Error");
}
ok = ok && double.TryParse(txtWeight.Text, out weight); // only returns the ok value if both the values are valid!
if (!ok)
{
MessageBox.Show("The weight value is invalid", "Error");
}
// cm --> m, ft --> in
if (bmiCalc.GetUnit() == UnitTypes.Metric)
height = height / 100.0; // cm --> m
else if (bmiCalc.GetUnit() == UnitTypes.American)
height = height * 12.0 + inch; //ft --> in
bmiCalc.SetHeight(height);
bmiCalc.SetWeight(weight);
return ok; // only returns the ok value if both the values are valid!
}
private bool ReadInputBMI() // Reads all the imputs
{
ReadName();
bool heightAndWeightOK = ReadHeightAndWeight();
ReadUnit();
return heightAndWeightOK;
}
private void ReadUnit()
{
if (rbtnMetric.Checked)
bmiCalc.SetUnit(UnitTypes.Metric);
else if (rbtnImperial.Checked)
bmiCalc.SetUnit(UnitTypes.American);
}
private void CalculateAndDisplayResults()
{
bool inputOK = ReadInputBMI(); // read the input values
if (inputOK)
{
double bmi = bmiCalc.CalculateBMI(bmiCalc.GetHeight(), bmiCalc.GetWeight(), bmiCalc.GetUnit());
resultBMIOutput.Text = bmi.ToString("f2");
}
}
private void ReadInputBMI_TextChanged(object sender, EventArgs e)
{
}
private void rbtnMetric_CheckedChanged(object sender, EventArgs e)
{
UpdateHeightText(); // runs the checking method
}
}
}
BMICalculator2.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assignment3
{
class BMICalculator2
{
private string name = "No Name";
private double height = 0;
private double weight = 0;
private UnitTypes unit;
public double CalculateBMI(double height, double weight, UnitTypes unitTypes)
{
double bmi = 0;
if (unitTypes == UnitTypes.Metric)
{
// Calculate BMI using metric values
bmi = weight / Math.Pow(height / 100, 2);
}
else if (unitTypes == UnitTypes.American)
{
// Calculate BMI using imperial values
bmi = (weight * 703) / Math.Pow(height, 2);
}
return bmi;
}
public string BMIWeightCategory() // BMI Values taken from Who website
{
double bmi = CalculateBMI(height, weight, unit);
string stringout = string.Empty;
if (bmi > 40)
stringout = "Overweight (Obesity Class III)";
else if (bmi >= 35 && bmi <= 39.9)
stringout = "Overweight (Obesity Class II)";
else if (bmi >= 30 && bmi <= 34.9)
stringout = "Overweight (Obesity Class I)";
else if (bmi >= 25 && bmi <= 29.9)
stringout = "Overweight (Pre-obese)";
else if (bmi >= 18.5 && bmi <= 24.9)
stringout = "Normal weight";
else if (bmi < 18.5)
stringout = "Underweight";
return stringout;
}
internal UnitTypes GetUnit()
{
return unit;
}
public double GetHeight()
{
return height;
}
public double GetWeight()
{
return weight;
}
// setters
public void SetHeight(double height)
{
}
public void SetUnit(UnitTypes unit)
{
}
public void SetWeight(double value)
{
if (value >= 0)
height = value;
}
}
}
UnitTypes
enum UnitTypes
{
Metric,
American
}
}
好吧,至少有一个问题是相当明显的:
public void SetHeight(double height)
{
}
public void SetUnit(UnitTypes unit)
{
}
public void SetWeight(double value)
{
if (value >= 0)
height = value;
}
SetHeight
和SetUnit
做nothing,SetWeight
做错事。
这些类型的错误在开始编程时很容易犯。出于这个原因,学习如何使用 debugger 是必不可少的。这使您可以单步执行程序,并在执行时检查字段和变量。这使您可以验证您的假设。为什么BMI为零?因为权重为零。为什么权重为零?等等等等
另见 Eric Lippert 的文章如何调试小程序