Reputation: 97
I am trying to make a very basic program.
I am trying to allow a user to enter their temperature into a text box and then if the temperature is below 15 the word cold will be displayed in a label in the color blue, if it is above 15 it will be display hot in the color red.
So far this is my Code:
namespace FinalTemperature
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
double addTemperature;
addTemperature = Double.Parse(txttemp.Text);
lblans.Text = addTemperature.ToString();
if (txttemp.Text.Trim() == "15")
{
lblans.ForeColor = System.Drawing.Color.Red;
}
if (txttemp.Text.Trim() == "14")
{
lblans.ForeColor = System.Drawing.Color.Blue;
}
}
}
}
So far my program just displays the temperature in red if its 15 and blue if its 14 and just outputs the number to the label, but at the moment no other numbers have an effect on the color.
Upvotes: 2
Views: 2728
Reputation: 43264
You are testing for 14 and 15 specifically. You'll need to convert the strings to ints and compare with >= and <=.
Upvotes: 1
Reputation:
Add a label in page,
<asp:label id="lbl" runat="server"></asp:label>,
// Use the condition given below:
if(Convert.ToInt32(txtTemp.Text)>15 )
{
lbl.Text= "HOT";
lbl.ForeColor= System.Drawing.Color.Red;
}
else
{
lbl.Text="COLD";
lbl.ForeColor= System.Drawing.Color.Blue;
}
The above code converts the string value to Int32, so that you can compare it with any numeric value you want, instead of taking the compared value as string. You cannot apply logical operator other than equals to , to string.
Upvotes: 2
Reputation: 2437
This answers your requirements. Convert the String (text) to Int (numbers), and compare the values.
protected void Button1_Click(object sender, EventArgs e)
{
if (Int32.Parse(txttemp.Text.Trim()) >= 15)
{
lblans.Text = "Hot";
lblans.ForeColor = System.Drawing.Color.Red;
}
else
{
lblans.Text = "Cold";
lblans.ForeColor = System.Drawing.Color.Blue;
}
}
Upvotes: 3
Reputation: 14153
You need to use the <=
, >=
, >
, or <
operators, used for checking if numbers are less than, more than, etc. You will also need to use the converted double for this.
// If LESS than 15
if (addTemperature < 15)
{
lblans.ForeColor = System.Drawing.Color.Blue;
}
// Else, meaning anything other is MORE than 15, you could also do:
// else if (addTemperature >= 15), but that wouldn't be needed in this case
else
{
lblans.ForeColor = System.Drawing.Color.Red;
}
The reason your previous code did not work was because you were only checking for 15 and 14, and not any other values.
As I was reading your question I also thought a ternary operator would be a perfect use for this.
lblans.ForeColor = addTemperature < 15 ? System.Drawing.Color.Blue : System.Drawing.Color.Red
Upvotes: 1
Reputation: 2030
lblans.ForeColor = addTemperature < 15 ? System.Drawing.Color.Blue : System.Drawing.Color.Red;
Upvotes: 1
Reputation: 73502
You're actually checking equality to "15" and "14" ignoring all other values.
Try this
if(addTemperature < 15)
{
lblans.ForeColor = System.Drawing.Color.Blue;
}
else
{
lblans.ForeColor = System.Drawing.Color.Red;
}
Upvotes: 2