-1

I was making a math game in C# but for some reason, the correct answer isn't the actual correct answer I'm clueless as to why. There's more code but I think this is all that's needed but tell me if you need more I'm new to this.

var rand = new Random();
        int randnum;
        randnum = rand.Next(1, 20);
        Num1.Text = randnum.ToString();
        var rand3 = new Random();
        int randnum2;
        randnum2 = rand3.Next(1, 20);
        Num2.Text = randnum2.ToString();

        int numTotalA = randnum + randnum2;
        int numTotalMi = randnum - randnum2;
        int numTotalMu = randnum * randnum2;
        int numTotalD = randnum / randnum2;
        string PlayAns = PlayerAns.Text;
        int PlayAnsint = Convert.ToInt32(PlayAns);
        
        var OperaTor = new List<string> { "+", "-", "*", "/" };

        var opRand = new Random();
        int opPicker = opRand.Next(0, OperaTor.Count);
        var opChose = (OperaTor[opPicker]);
        Oper.Text = opChose;
        int Score = 0;`

1 Answers1

-1

Store your computed answers in a dictionary.

private Dictionary<string, int> Result = new Dictionary<string, int>(); // at the class level.

You can add them to the dictionary.

        Result["+"] = randnum + randnum2;
        Result["-"] = randnum - randnum2;
        Result["*"] = randnum * randnum2;
        Result["/"] = randnum / randnum2;

In your function that gets called after user answers the question, you can make the comparision.

UpdateScoreCount(int userAnswer)
{
    Score = Result[this.OpChose] == userAnswer ? Score + 1 : Score;
}
dotcoder
  • 2,828
  • 10
  • 34
  • 50