When entering the temperature value, I must determine the type of climate

0

When entering the temperature value, I must determine the type of climate. must be done with Windows forms.

  

Temperature 10 and = 21 and 30 = Tropical

This is the code I have:

 private void btnAceptar_Click(object sender, EventArgs e)
        {
            if (txtTipoClima.Text == string.Empty)
            {
                MessageBox.Show("Debe ingresar la Temperatura (Solo Números)", "Mensaje del Sistema", MessageBoxButtons.OK);
            }
            else
            {
                if (temperatura <10)
                {

                    MessageBox.Show("Frio", "Mensaje del Sistema", MessageBoxButtons.OK);
                }
                else
                {
                    if (temperatura >10 && temperatura <= 20)
                    {
                        MessageBox.Show("Nublado", "Mensaje del Sistema", MessageBoxButtons.OK);
                    }
                    else
                    {
                        if (temperatura >= 21 && temperatura <= 30)
                        {
                            MessageBox.Show("Calor", "Mensaje del Sistema", MessageBoxButtons.OK);
                        }
                        else
                        {
                            if (temperatura > 30)
                            {
                                MessageBox.Show("Tropical", "Mensaje del Sistema", MessageBoxButtons.OK);
                            }
                        }
                    }
                }
            }
        }


I can not define all types of temperature, only this fragment of the code works:

if (temperatura <10)
                    {

                        MessageBox.Show("Frio", "Mensaje del Sistema", MessageBoxButtons.OK);
    
asked by Liszt Cea 11.11.2017 в 18:50
source

3 answers

1

Make sure that when you enter this method, temperature has the value you entered in the text box.

What it seems is that you do not assign the value to it and it is always 0, which always gets you into the first if. Just enter the beam method:

temperatura = Int32.Parse(txtTipoClima.Text); //Asumo que es un entero, sino pásalo a double

Now, taking the temperature value if you can do the comparisons (better using else if):

temperatura = Int32.Parse(txtTipoClima.Text);

if (txtTipoClima.Text == string.Empty){
    MessageBox.Show("Debe ingresar la Temperatura (Solo Números)", "Mensaje del Sistema", MessageBoxButtons.OK);
}else if (temperatura <=10){
          MessageBox.Show("Frio", "Mensaje del Sistema", MessageBoxButtons.OK);
}else if (temperatura >10 && temperatura <= 20){
          MessageBox.Showw("Nublado", "Mensaje del Sistema", MessageBoxButtons.OK);
}else if (temperatura >= 21 && temperatura <= 30){
          MessageBox.Show("Calor", "Mensaje del Sistema", MessageBoxButtons.OK);
}else if (temperatura > 30){        
      MessageBox.Show("Tropical", "Mensaje del Sistema", MessageBoxButtons.OK);
}
    
answered by 13.11.2017 в 13:35
0

Your code works fine maybe the error is when you save in the temperature variable, it should be this way

int temperatura = int.Parse(txtTipoClima.Text);

The only error is that if you enter 10 it does not enter into any condition if you could change the first one in such a way that it says less than or equal to 10

if (temperatura <= 10){
    
answered by 11.11.2017 в 19:28
0

You should know that there is a statement to compare complementary results for the if this is otherwise if you try with this code, (I do not have ID # of C #) '

private void btnAceptar_Click(object sender, EventArgs e){
    if (txtTipoClima.Text == string.Empty){
        MessageBox.Show("Debe ingresar la Temperatura (Solo Números)", "Mensaje del Sistema", MessageBoxButtons.OK);
    }else if (temperatura <10){
              MessageBox.Show("Frio", "Mensaje del Sistema", MessageBoxButtons.OK);
    }else if (temperatura >10 && temperatura <= 20){
              MessageBox.Showw("Nublado", "Mensaje del Sistema", MessageBoxButtons.OK);
    }else if (temperatura >= 21 && temperatura <= 30){
              MessageBox.Show("Calor", "Mensaje del Sistema", MessageBoxButtons.OK);
    }else if (temperatura > 30){        
          MessageBox.Show("Tropical", "Mensaje del Sistema", MessageBoxButtons.OK);
    }
}
    
answered by 11.11.2017 в 19:14