我正在尝试制作一个非常基本的程序。
我试图让用户在一个文本框中输入温度,然后如果温度低于15,则感冒一词将以蓝色显示在标签中,如果温度高于15,则将其显示为高温。红色的。
到目前为止,这是我的代码:
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;
}
}
}
}
到目前为止,我的程序仅将温度显示为红色(如果温度为15),将蓝色显示为14(温度),并且仅将数字输出到标签,但是目前尚无其他数字对颜色产生影响。
这可以满足您的要求。将字符串(文本)转换为整数(数字),然后比较这些值。
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;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句