Can anyone help me check my BMI Calculator? (C#)

耗尽温柔 提交于 2020-01-21 18:58:45

问题


I just started programming a week ago and my first assignment was to code a BMI calculator.

It is supposed to look like this when launched:

    BMI Calculator
    Your weight in kg: x 
    Your height in cm: x
    Gender (m/f): x

-> You are underweight/normal/overweight

Here is my code so far:

            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();

I'm not sure what's wrong with my code but whenever I enter 60kgs, 170cm and male, I get overweight, even though I should get normal. Same thing with anything above 10kgs actually.

PS: I'm really a beginner at programming so I apologize for my command of the programming lingo.

And for your convenience:

http://i.stack.imgur.com/admqr.png

Thanks in advance!


回答1:


When you do:

BMI = kg / (m / 100) * (m / 100);

And m is an int, you'd be doing integer division, in which case 170 / 100 = 1. And as User1551892 pointed out, you'll need to be a bit more specific about the order of calculations.

Try:

double BMI = kg / ( ( m / 100.0 ) * ( m / 100.0 ) );

That will force it to do floating point division and should get you better results.

Also, you could use Math.Pow to avoid having m / 100.0 twice:

double BMI = kg / Math.Pow( m / 100.0, 2 );



回答2:


Please check this line:

BMI = kg / (m / 100) * (m / 100);

it should be like this:

BMI = kg / ((m / 100) * (m / 100)) ;



回答3:


FYI new stack's exchange portal has opened : codereview.stackexchange.com

It should be better place to ask for review




回答4:


the calculation is off though for BMI. it is (weight/height)/height so int bmi = (kg/(m/100))/(m/100)




回答5:


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");
    }
}



回答6:


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;
        }
}
}


来源:https://stackoverflow.com/questions/19478961/can-anyone-help-me-check-my-bmi-calculator-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!