我刚开始编程一周前,我的第一个任务是编写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
提前致谢!
答案 0 :(得分:3)
当你这样做时:
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 );
答案 1 :(得分:2)
请检查此行:
BMI = kg / (m / 100) * (m / 100);
它应该是这样的:
BMI = kg / ((m / 100) * (m / 100)) ;
答案 2 :(得分:1)
FYI新堆栈的交换门户网站已开通:codereview.stackexchange.com
应该是更好的地方要求审核
答案 3 :(得分:1)
尽管对于BMI,该计算已关闭。是(体重/身高)/身高 所以 int bmi =(kg /(m / 100))/(m / 100)
答案 4 :(得分:0)
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");
}
}
答案 5 :(得分:0)
在每个IF语句中检查您的性别变量...
答案 6 :(得分:-1)
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;
}
}
}