输入格式不正确
protected void BtnCal_click(object sender, EventArgs e)
{
{ double result = 0;
double width;
double length;
double radius;
double breath;
radius = (Convert.ToDouble(TextRadius.Text));
length = (double.Parse(TextLength.Text));
breath = (double.Parse(TextBreath.Text));
width = (Convert.ToDouble(TextWidth.Text));
if (DropDownShapes.SelectedValue == "cir")
{ double pi = 3.142;
result = radius * radius * pi;
TextWidth.Text = "0";
TextLength.Text = "0";
TextBreath.Text = "0";
}
else if (DropDownShapes.SelectedValue == "tri")
{ result = length * breath * 0.5;
TextWidth.Text = "0";
TextRadius.Text = "0";
}
else if (DropDownShapes.SelectedValue == "rec")
{ result = length * width;
TextRadius.Text = "0";
TextLength.Text = "0";
TextBreath.Text = "0"
}
else
{
TextArea.Text = result.ToString();
}
}
}
不确定问题到底是什么,但也许你有问题,在你的一个TextBox中没有双重值。为了解决这个问题,您可以改用double.tryparse Convert.ToDouble或double.parse
double.TryParse(TextRadius.Text, out radius);
double.TryParse(TextLength.Text, out length);
double.TryParse(TextBreath.Text, out breath);
double.TryParse(TextWidth.Text, out width);
Convert.ToDouble和double.Parse将抛出exeption而double.TryParse将如果无法解析返回false。
而且answerd这里: c# Double.TryParse or Convert.ToDouble
最后我检查了一下'TryParse'有多个参数,它返回'bool's来表示成功。 –
这些代码行不会编译。 'double.TryParse'不仅仅是一个参数。 –
你是对的,谢谢你的提示......我采纳了答案。不知道它是否有帮助。关于来自ibtihaj khan的代码还有一些问题...例如为什么结果只显示在其他等等... – mitch
随着问题是? –