我使用了这段代码,但似乎无效。是否有人回答
private void txtBfrLeft1_Leave(object sender, EventArgs e)
{
if (txtBfrRight1.Text != "" || txtBfrLeft1.Text != "")
{
int befrAvg1 = (Convert.(txtBfrRight1.Text) + Convert.ToDouble(txtBfrLeft1.Text)) / 2;
txtBfrAvg1.Text = befrAvg1.ToString();
}
}
答案 0 :(得分:3)
试试这个:
double befrAvg1 = (new []
{
double.Parse(txtBfrRight1.Text),
double.Parse(txtBfrLeft1.Text)
}).Average();
或者如果您确实希望结果为整数,那么:
int befrAvg1 = (int)((new []
{
double.Parse(txtBfrRight1.Text),
double.Parse(txtBfrLeft1.Text)
}).Average());