Guest User

Untitled

a guest
Jun 21st, 2012
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5.  
  6. public enum Operators {ADD, SUBTRACT, MULTIPLY, DIVIDE}
  7.  
  8. namespace MathQuizWeb.Classes
  9. {
  10.     public class Question
  11.     {
  12.         private Random __Rnd = new Random();
  13.  
  14.         public int __Answer       { get; set; }
  15.         public int __LeftOperand  { get; set; }
  16.         public int __RightOperand { get; set; }
  17.         public string __Operator  { get; set; }
  18.  
  19.         private static string[] _Operators = Enum.GetNames(typeof(Operators));
  20.  
  21.  
  22.         public Question()
  23.         {
  24.             //Select a random operator            
  25.             int _Size = _Operators.Length;
  26.             this.__Operator = _Operators[__Rnd.Next(0, _Size)];
  27.             Console.WriteLine(__Operator);
  28.             Initialize();
  29.         }
  30.  
  31.         public Question(string Operator)
  32.         {
  33.             this.__Operator = Operator;
  34.             Initialize();
  35.         }
  36.  
  37.         private void Initialize()
  38.         {
  39.             if (__Operator.Equals("ADD"))
  40.             {
  41.                 this.__LeftOperand  = __Rnd.Next(25);
  42.                 this.__RightOperand = __Rnd.Next(25);
  43.                 this.__Answer = this.__LeftOperand + this.__RightOperand;
  44.             }
  45.             else if (__Operator.Equals("SUBTRACT"))
  46.             {
  47.                 this.__LeftOperand = __Rnd.Next(5, 25);
  48.                 this.__RightOperand = __Rnd.Next(__LeftOperand); //Make smaller than it's Left hand counterpart
  49.                 this.__Answer = this.__LeftOperand - this.__RightOperand;
  50.             }
  51.             else if (__Operator.Equals("DIVIDE"))
  52.             {
  53.                 this.__LeftOperand = __Rnd.Next(1, 10);
  54.                 this.__RightOperand = __Rnd.Next(1, 10);
  55.                 this.__Answer = this.__LeftOperand / this.__RightOperand;
  56.             }
  57.             else if (__Operator.Equals("MULTIPLY"))
  58.             {
  59.                 this.__LeftOperand = __Rnd.Next(2, 11);
  60.                 this.__RightOperand = __RightOperand * __Rnd.Next(1, 10); //Multiple, to ensure that the numbers are divisible
  61.                 this.__Answer = this.__LeftOperand * this.__RightOperand;
  62.             }
  63.         }
  64.  
  65.         public string ToString()
  66.         {
  67.             return this.__LeftOperand + " " + this.__Operator + " " + this.__RightOperand + " = " + this.__Answer;
  68.         }
  69.        
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment