-1

I'm trying to make my program calculate the % of correct vs. incorrect answers. When I run the program and enter the first correct value, it shows 100%. However, when I type in a wrong answer it shows the % as 0.00% when it should be 50% since there was 1 right and 1 wrong. How do I get this to work properly. Here is my code:

private void getCalc()
{
    lblPercent.Text = (intCorrect / (intCorrect + intIncorrect)).ToString("P");
    getRan();
    lblFirstNum.Text = intNum1.ToString();
    lblSecNum.Text = intNum2.ToString();
    txtAnswer.Clear();
    txtAnswer.Focus();
}


private void getRan()
{
    Random myRandom = new Random();
    intNum1 = myRandom.Next(0, 10);
    intNum2 = myRandom.Next(0, 10);
}   

Method3:

intCorrect = 0;
intIncorrect = 0;

if (txtAnswer.Text == (intNum1 * intNum2).ToString())
{
    intCorrect += 1;
        lblCorrect.Text = intCorrect.ToString();
}
else
{
    intIncorrect += 1;
    lblIncorrect.Text = intIncorrect.ToString();
}

getCalc();

1 Answers1

0

You need to cast your values to a decimal type (float, double, or decimal depending on precision). Doing division with ints will drop the decimal value.

lblPercent.Text = ((double)intCorrect / (intCorrect + intIncorrect)).ToString("P");
Cyral
  • 13,999
  • 6
  • 50
  • 90