[英]how to take average of two text box values C#
我使用了這段代碼,但似乎不起作用。 有人有答案嗎
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();
}
}
嘗試這個:
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());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.