trying to impliment non / by 0

This commit is contained in:
EggMan20339 2024-02-01 11:03:13 -05:00
parent 6a64647813
commit 08bc2c16e1
2 changed files with 69 additions and 5 deletions

View File

@ -211,6 +211,7 @@
this.btnShowCorrectAnswer.TabIndex = 7;
this.btnShowCorrectAnswer.Text = "Show Correct";
this.btnShowCorrectAnswer.UseVisualStyleBackColor = true;
this.btnShowCorrectAnswer.Click += new System.EventHandler(this.btnShowCorrectAnswer_Click);
//
// btnChkAnswer
//

View File

@ -124,7 +124,7 @@ namespace ElementaryMathematics
private ErrorProvider ep1 = new ErrorProvider();
private decimal Calculate(decimal a, decimal b, decimal c)
private decimal Calculate(decimal a, decimal b)
{
decimal answer = 0;
@ -147,8 +147,16 @@ namespace ElementaryMathematics
hasTried = true;
}else if (isDivide)
{
answer = a / b;
hasTried = true;
if (b != 0)
{
answer = a / b;
}
else
{
throw new Exception();
}
}
else
{
@ -158,7 +166,7 @@ namespace ElementaryMathematics
}
catch (Exception e)
{
MessageBox.Show("Whoops!");
ep1.SetError(btnChkAnswer, "Calculation Error");
}
@ -209,7 +217,7 @@ namespace ElementaryMathematics
try
{
if (c == Calculate(a,b,c))
if (c == Calculate(a,b))
{
Win();
@ -296,12 +304,14 @@ namespace ElementaryMathematics
UpdateButtons();
rdoAddition.Select();
lblNumCorrect.Text = "0";
lblNumIncorrect.Text = "0";
}
private void UpdateButtons()
{
if (txtA.TextLength > 0 && txtB.TextLength > 0 && txtC.TextLength > 0)
if (txtA.TextLength > 0 && txtB.TextLength > 0 && txtC.TextLength > 0 && !hasTried)
{
btnChkAnswer.Enabled = true;
@ -344,5 +354,58 @@ namespace ElementaryMathematics
}
}
private void btnShowCorrectAnswer_Click(object sender, EventArgs e)
{
ep1.Clear();
decimal a = 0;
decimal b = 0;
decimal c = 0;
try
{
a = decimal.Parse(txtA.Text);
try
{
b = decimal.Parse(txtB.Text);
try
{
lblMessage.Text = Calculate(a, b).ToString();
}
catch (Exception exception)
{
ep1.SetError(btnChkAnswer, "Calculation Error");
}
}
catch (Exception exception)
{
ep1.SetError(txtB, "Invalid Value");
}
}
catch (Exception exception)
{
ep1.SetError(txtA, "Invalid Value");
}
UpdateButtons();
}
}
}