當前位置:編程學習大全網 - 源碼破解 - C語言。。messagebox用法

C語言。。messagebox用法

窗體上放置三個TextBox,分別輸入a,b,c的值,控件命名:tbA,tbB,tbC

再放壹個Button,設置Text為:求解,其單擊後臺代碼如下:

private void button1_Click(object sender, EventArgs e)

{

double a = 0;

double b = 0;

double c = 0;

try

{

if (tbA.Text.Length == 0)

{

MessageBox.Show("請輸入a的值", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

return;

}

a = Convert.ToDouble(tbA.Text);

}

catch

{

MessageBox.Show("您輸入的a的值不是壹個數字,請重新輸入", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

tbA.Focus();

return;

}

try

{

if (tbB.Text.Length == 0)

{

MessageBox.Show("請輸入b的值", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

return;

}

b = Convert.ToDouble(tbB.Text);

}

catch

{

MessageBox.Show("您輸入的b的值不是壹個數字,請重新輸入", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

tbB.Focus();

return;

}

try

{

if (tbC.Text.Length == 0)

{

MessageBox.Show("請輸入c的值", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

return;

}

c = Convert.ToDouble(tbC.Text);

}

catch

{

MessageBox.Show("您輸入的c的值不是壹個數字,請重新輸入", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

tbC.Focus();

return;

}

if (a == 0)

{

if (b == 0)

{

if (c == 0)

{

MessageBox.Show(string.Format("方程{0}x^2+{1}x+{2}=0的解為 x={3}", a, b, c, "任意實數"), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

else

{

MessageBox.Show(string.Format("方程{0}x^2+{1}x+{2}=0無實數解", a, b, c), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

}

else

{

MessageBox.Show(string.Format("方程{0}x^2+{1}x+{2}=0的解為 x={3}", a, b, c, -c / b), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

}

else

{

double delta = b * b - 4 * a * c;

if (delta < 0)

{

MessageBox.Show(string.Format("方程{0}x^2+{1}x+{2}=0無實數解", a, b, c), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

else

{

MessageBox.Show(string.Format("方程{0}x^2+{1}x+{2}=0的解為 x1={3} , x2={4}", a, b, c, (-b + System.Math.Sqrt(delta)) / 2 / a, (-b - System.Math.Sqrt(delta)) / 2 / a), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

}

}

  • 上一篇:穿梭陰陽戀詳細介紹
  • 下一篇:- - 漫畫妖狐x仆ss結局沒看懂
  • copyright 2024編程學習大全網