谁能帮我检查一下我的BMI计算器? (C#)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了谁能帮我检查一下我的BMI计算器? (C#)相关的知识,希望对你有一定的参考价值。
我刚刚开始编程一周前,我的第一个任务是编写BMI计算器。
推出时应该看起来像这样:
BMI Calculator
Your weight in kg: x
Your height in cm: x
Gender (m/f): x
-> You are underweight/normal/overweight
到目前为止,这是我的代码:
Console.WriteLine("BMI Calculator");
Console.WriteLine("===========");
Console.WriteLine();
Console.Write("Weight in kg: ");
int kg;
kg = Convert.ToInt32(Console.ReadLine());
Console.Write("Height in cm: ");
int m;
m = Convert.ToInt32(Console.ReadLine());
Console.Write("Gender (m/f):");
string Geschlecht = Console.ReadLine();
int BMI;
BMI = kg / (m / 100) * (m / 100);
if (BMI < 19 & Gender == "f")
{ Console.WriteLine("-> Underweight"); }
if (BMI >= 19 & BMI <= 24 & Gender == "f")
{ Console.WriteLine("-> Normal"); }
if (BMI > 24 & Geschlecht == "f")
{ Console.WriteLine("-> Overweight"); }
if (BMI < 20 & Gender == "m")
{ Console.WriteLine("-> Underweight"); }
if (BMI >= 20 & BMI <= 25 & Gender == "m")
{ Console.WriteLine("-> Normal"); }
if (BMI > 25 & Gendert == "m")
{ Console.WriteLine("-> Overweight"); }
Console.ReadLine();
我不确定我的代码有什么问题,但每当我输入60公斤,170厘米和男性时,我都会超重,即使我应该恢复正常。实际上超过10公斤的东西也是如此。
PS:我真的是编程的初学者,所以我为编程术语的命令道歉。
为了您的方便:
http://i.stack.imgur.com/admqr.png
提前致谢!
答案
当你这样做时:
BMI = kg / (m / 100) * (m / 100);
而m
是一个int
,你将做整数除法,在这种情况下170 / 100 = 1
。正如User1551892指出的那样,您需要更加具体地了解计算顺序。
尝试:
double BMI = kg / ( ( m / 100.0 ) * ( m / 100.0 ) );
这将迫使它做浮点除法,应该会得到更好的结果。
此外,您可以使用Math.Pow
避免m / 100.0
两次:
double BMI = kg / Math.Pow( m / 100.0, 2 );
另一答案
请检查此行:
BMI = kg / (m / 100) * (m / 100);
它应该是这样的:
BMI = kg / ((m / 100) * (m / 100)) ;
另一答案
仅供参考FYI新堆栈的交换门户网站:codereview.stackexchange.com
应该是更好的地方要求审查
另一答案
LOGIX.ASPX
using System;
using System.Configuration;
using System.Data.SqlClient;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnLogin_Click(object sender, EventArgs e)
{
bool blnLoginOK = false;
//Verbindung zur DB herstellen
SqlConnection conDBTodo = new SqlConnection(ConfigurationManager.ConnectionStrings["conStrLogin"].ConnectionString);
//SqlCommand vorbereiten, Verbindung zur Tabelle
SqlCommand comDemoSelect = new SqlCommand();
comDemoSelect.Connection = conDBTodo;
//comDemoSelect.CommandType = comm
comDemoSelect.CommandText = "SELECT ID FROM tabUser WHERE Email=@Email AND Passwort=@Passwort";
comDemoSelect.Parameters.AddWithValue("@Email", this.txtMail.Text);
comDemoSelect.Parameters.AddWithValue("@Passwort", this.txtPW.Text);
comDemoSelect.Connection.Open();
//Datareader um Daten anzuzeigen
SqlDataReader drTodo = comDemoSelect.ExecuteReader();
//Datenausgabe in Labelfeld mit Datareader
blnLoginOK = drTodo.HasRows; //Sind Datensätze vorhanden?
//Schliessen aller Verbindungen
drTodo.Dispose();
drTodo.Close();
comDemoSelect.Dispose();
comDemoSelect.Connection.Close();
conDBTodo.Dispose();
conDBTodo.Close();
if (blnLoginOK == true)
{
Session["LoggedIn"] = "true";
Response.Redirect("Secure.aspx");
}
else
{
Response.Redirect("Login.aspx");
}
}
protected void btnReg_Click(object sender, EventArgs e)
{
Response.Redirect("Registration.aspx");
}
protected void btnWeg_Click(object sender, EventArgs e)
{
//Verbindung zur DB herstellen
SqlConnection conDBTodoDelete = new SqlConnection(ConfigurationManager.ConnectionStrings["conStrLogin"].ConnectionString);
//SqlCommand vorbereiten, Verbindung zur Tabelle mit SQL - Befehl
SqlCommand comDemoDelete = new SqlCommand("DELETE FROM tabUser WHERE Email = @Email AND Passwort = @Passwort");
comDemoDelete.Connection = conDBTodoDelete;
comDemoDelete.Parameters.AddWithValue("@Email", this.txtMail.Text);
comDemoDelete.Parameters.AddWithValue("@Passwort", this.txtPW.Text);
comDemoDelete.Connection.Open();
comDemoDelete.ExecuteNonQuery();
comDemoDelete.Connection.Close();
comDemoDelete.Dispose();
conDBTodoDelete.Dispose();
conDBTodoDelete.Close();
txtMail.Text = "Sie haben nun Ihren Account gelöscht!";
return;
}
}
REGISTRATION.ASPX
using System;
using System.Configuration;
using System.Data.SqlClient;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnRegistrierung_Click(object sender, EventArgs e)
{
//Verbindung zur DB herstellen
SqlConnection conDBTodo = new SqlConnection(ConfigurationManager.ConnectionStrings["conStrLogin"].ConnectionString);
//SqlCommand vorbereiten, Verbindung zur Tabelle mit SQL - Befehl
SqlCommand comDemoInsert = new SqlCommand("INSERT INTO tabUser(Benutzername, Email, Passwort) VALUES (@Benutzername, @Email, @Passwort)");
comDemoInsert.Connection = conDBTodo;
comDemoInsert.Parameters.AddWithValue("@Benutzername", this.txtBen.Text);
comDemoInsert.Parameters.AddWithValue("@Email", this.txtMail.Text);
comDemoInsert.Parameters.AddWithValue("@Passwort", this.txtPW.Text);
comDemoInsert.Connection.Open();
comDemoInsert.ExecuteNonQuery();
comDemoInsert.Connection.Close();
comDemoInsert.Dispose();
conDBTodo.Dispose();
conDBTodo.Close();
Response.Redirect("Login.aspx");
}
protected void btnBack_Click(object sender, EventArgs e)
{
Response.Redirect("Login.aspx");
}
}
另一答案
protected void btnGoo_Click(object sender, EventArgs e)
{
this.txtErgebnis.BackColor = System.Drawing.Color.White;
this.txtErgebnis.Text = "";
this.txtErgebnis.ForeColor = System.Drawing.Color.White;
//variablen Definieren
double dblGroesse = 0;
double dblGewicht = 0;
double dblErgebnis = 0;
if (Double.TryParse(txtGroesse.Text, out dblGroesse) == false) {
txtGroesse.Text = "Geben Sie ihre Grösse ein";
return;
}
if (Double.TryParse(txtGewicht.Text, out dblGewicht) == false)
{
txtGewicht.Text = "Geben Sie ihr Gewicht ein";
return;
}
dblErgebnis = dblGewicht / (dblGroesse * dblGroesse);
txtErgebnis.Text = Convert.ToString (dblErgebnis);
if (dblErgebnis <= 16)
{
this.txtErgebnis.BackColor = System.Drawing.Color.Coral;
this.txtErgebnis.ForeColor = System.Drawing.Color.LightGoldenrodYellow;
}
else if (dblErgebnis <= 25)
{
this.txtErgebnis.BackColor = System.Drawing.Color.DarkRed;
this.txtErgebnis.ForeColor = System.Drawing.Color.DarkSlateGray;
}
}
}
以上是关于谁能帮我检查一下我的BMI计算器? (C#)的主要内容,如果未能解决你的问题,请参考以下文章