Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- public enum Operators {ADD, SUBTRACT, MULTIPLY, DIVIDE}
- namespace MathQuizWeb.Classes
- {
- public class Question
- {
- private Random __Rnd = new Random();
- public int __Answer { get; set; }
- public int __LeftOperand { get; set; }
- public int __RightOperand { get; set; }
- public string __Operator { get; set; }
- private static string[] _Operators = Enum.GetNames(typeof(Operators));
- public Question()
- {
- //Select a random operator
- int _Size = _Operators.Length;
- this.__Operator = _Operators[__Rnd.Next(0, _Size)];
- Console.WriteLine(__Operator);
- Initialize();
- }
- public Question(string Operator)
- {
- this.__Operator = Operator;
- Initialize();
- }
- private void Initialize()
- {
- if (__Operator.Equals("ADD"))
- {
- this.__LeftOperand = __Rnd.Next(25);
- this.__RightOperand = __Rnd.Next(25);
- this.__Answer = this.__LeftOperand + this.__RightOperand;
- }
- else if (__Operator.Equals("SUBTRACT"))
- {
- this.__LeftOperand = __Rnd.Next(5, 25);
- this.__RightOperand = __Rnd.Next(__LeftOperand); //Make smaller than it's Left hand counterpart
- this.__Answer = this.__LeftOperand - this.__RightOperand;
- }
- else if (__Operator.Equals("DIVIDE"))
- {
- this.__LeftOperand = __Rnd.Next(1, 10);
- this.__RightOperand = __Rnd.Next(1, 10);
- this.__Answer = this.__LeftOperand / this.__RightOperand;
- }
- else if (__Operator.Equals("MULTIPLY"))
- {
- this.__LeftOperand = __Rnd.Next(2, 11);
- this.__RightOperand = __RightOperand * __Rnd.Next(1, 10); //Multiple, to ensure that the numbers are divisible
- this.__Answer = this.__LeftOperand * this.__RightOperand;
- }
- }
- public string ToString()
- {
- return this.__LeftOperand + " " + this.__Operator + " " + this.__RightOperand + " = " + this.__Answer;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment