Pls modify this code

Jul 27 2009 4:07 AM
Hi all, I am writing dice roller program in c sharp console. I am giving two input 1)Enter the size of the dice and 2)Enter how times you want to play. Suppose dice size is 6 and 10 times i have played. Output is coming: 1 was rolled 2 times 2 was rolled 7 times 3 was rolled 8 times 4 was rolled 7 times 5 was rolled 4 times 6 was rolled 5 times Total: 33 (its not fixed for every execution this no will be changed) But my requirement was this total always should be no of playing times. Here i am playing 10 times so the total should be 10 not 33. It should happen for every new numbers...if i play 100 times sum or total should be 100 only not any other number. rest all will be remain same, in my programing not getting the expected sum. Pls somebody modify it. Here is my code: Dice.cs: public class Dice { Int32 _DiceSize; public Int32 _NoOfDice; public Dice(Int32 dicesize) { this._DiceSize = dicesize; } public string Roll() { if (_DiceSize<= 0) { throw new ApplicationException("Dice Size cant be less than 0 or 0"); } if (_NoOfDice <= 0) { throw new ApplicationException("No of dice cant be less than 0 or 0"); } Random rnd = new Random(); Int32[] roll = new Int32[_DiceSize]; for (Int32 i = 0; i < _DiceSize; i++) { roll[i] = rnd.Next(1,_NoOfDice); } StringBuilder result = new StringBuilder(); Int32 Total = 0; Console.WriteLine("Rolling......."); for (Int32 i = 0; i < roll.Length; i++) { Total += roll[i]; result.AppendFormat("{0}:\t was rolled\t{1}\t times\n", i + 1, roll[i]); } result.AppendFormat("\t\t\t......\n"); result.AppendFormat("TOTAL: {0}", Total); return result.ToString(); } } class Program { static void Main(string[] args) { Console.WriteLine("Enter no of dice size"); int dicesize = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("How many times want to play"); int noofplay=Convert.ToInt32(Console.ReadLine()); Dice obj = new Dice(dicesize); obj._NoOfDice = noofplay; obj.Roll(); Console.WriteLine(obj.Roll()); Console.WriteLine("Press enter to exit"); Console.ReadKey(); } } Thanks, Sumit

Answers (2)