所以基本上我試圖讓Windows窗體應用程序來計算4個測試等級的平均值。但是,在一些課堂上,我可能只有3個測試。如果沒有四個測試文本框的值,程序將不會計算平均值。我試圖找出一個辦法,如果第四個文本框有什麼也沒有編碼,只是忽略它,計算其他3我的代碼如下:Textbox空問題
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace GradeCalc
{
public partial class Grades : Form
{
public Grades()
{
InitializeComponent();
}
private void Submit_Click(object sender, EventArgs e)
{
double test1;
double test2;
double test3;
double test4;
double average4;
double average3;
//Here, I'm trying to achieve:
//if (t4 is empty)
//{find the average of the first 3 textboxes}
if (t4.Text == null)
{
test1 = double.Parse(t1.Text);
test2 = double.Parse(t2.Text);
test3 = double.Parse(t3.Text);
average3 = ((test1 + test2 + test3)/3);
tavg.Text = average3.ToString("00.00");
}
//Here, I'm trying to achieve:
//if (t4 is not empty)
//{ calculate the average of all 4 textboxes}
else
if (t4.Text != null)
{
test1 = double.Parse(t1.Text);
test2 = double.Parse(t2.Text);
test3 = double.Parse(t3.Text);
test4 = double.Parse(t4.Text);
average4 = ((test1 + test2 + test3 + test4)/4);
tavg.Text = average4.ToString("00.00");
}
}
}
}
我沒有很多的經驗與C#,所以任何幫助,將不勝感激。