我对c#很新,我目前卡住了.. 看看我的程序
private void btn_calculate_Click(object sender, EventArgs e)
{
double[] credit = { 7.0, 6.0, 2.0 };
String[] grade = { txt_IT.Text, txt_IFA.Text, txt_PPD .Text};
double[] GPA = new double[3];
bool blank = true;
Double result = 0;
for(int i = 0; i<GPA.Length;i++)
{
if (grade[i] == "A" || grade[i] == "a")
{
GPA[i] = 4.0;
}
if (grade[i] == "B" || grade[i] == "b")
{
GPA[i] = 3.0;
}
if (grade[i] == "C" || grade[i] == "c")
{
GPA[i] = 2.0;
}
if (grade[i] == "D" || grade[i] == "d")
{
GPA[i] = 1.0;
}
if (grade[i] == "E" || grade[i] == "e")
{
GPA[i] = 1.0;
}
if (grade[i] == "F" || grade[i] == "f")
{
GPA[i] = 0.0;
}
if (grade[i] == "" && blank)
{
blank = false;
if (txt_IT.Text == "")
{
for (int print = 0; print < GPA.Length; print++)
{
result += credit[print] / 8 * GPA[print];
}//for print
txt_result.Text = result.ToString("0.000");
}//nested if
if (txt_IFA.Text == "")
{
for (int print = 0; print < GPA.Length; print++)
{
result += credit[print] / 9 * GPA[print];
}//for print
txt_result.Text = result.ToString("0.000");
}//nested if
if (txt_PPD.Text == "")
{
for (int print = 0; print < GPA.Length; print++)
{
result += credit[print] / 13 * GPA[print];
}//for print
txt_result.Text = result.ToString("0.000");
}//nested if
}//if
}//for GPA
for (int print = 0; print < GPA.Length; print++)
{
result += credit[print] / 15 * GPA[print];
}//for print
if (blank == true)
{
txt_result.Text = result.ToString("0.000");
}
}
private void btn_back_Click(object sender, EventArgs e)
{
Form1 data = new Form1();
this.Hide();
data.ShowDialog();
this.Close();
}
}
}
这有点正确,但我的老师想要它&#34;特别&#34; 他想要一个空白。
当我这样做时,结果是0.任何人都可以帮忙吗?
答案 0 :(得分:2)
我认为您最好使用String.IsNullOrEmpty(grade[i])
答案 1 :(得分:1)
此时,等级[i]可能为“空”&#39;所以你应该检查if ((grade[i] == null ||grade[i] == "") && blank)
另外为了清楚起见,我建议使用if (grade[i].ToUpper() == "A")