Recently started basic programming in uni and I am struggling with c#. One of my lab questions was write a dice program, i will write the exact question below. Because of anxiety I cannot ask the lecturer for help in class so thats why I am here. I am unsure of how to make or even start this program hopefully I can study the correct code to help me in future.

Here is the question
Write a program that rolls a dice(ie generates a random number between 1 and 6) and counts how many times the dice is rolled until it gets a 6. The program will ask the user if they want to continue by entering a positive number of terminate by entering a "0". The program displays the average number of rolls needed to get a six and the number of sixes that average was based on. The code below will help you generate a random number

We were given this piece of code for starter

Random rndm = new Random();


int number = rndm = rndm.Next(6)+1;

Recommended Answers

All 8 Replies

Will this be a windows application?

I did a simple example code in Console. Code simulates throwing dices by just pressing any key, and it does all you asked for.
To make this a win application try to use this code, but it should be a lot easier, because you wont need those boolean flages( two while loops). There user starts a new dice throw.

Check it out:

class Program
    {
        static void Main(string[] args)
        {
            List<int> listOfDices = new List<int>();
            Random r = new Random();
            bool bQuit = false;
            int dice;

            while (!bQuit)
            {
                Console.WriteLine("Please throw a new dice: just press any key to simulate.");
                string a = Console.ReadLine(); //not important just to  let user to stop the program
                dice = r.Next(1, 7);
                Console.WriteLine(String.Format("You throw: {0}", dice));
                listOfDices.Add(dice);
                if (dice == 6)
                {
                    Console.WriteLine("You got the six finally!!");
                    Console.WriteLine("---- Information: ----");
                    Console.WriteLine(String.Format("The number six was thrown in {0} arrempt.", GetAttempt(listOfDices.Count)));
                    Console.WriteLine(String.Format("The average all throws is {0}.", CalculateAverage(listOfDices)));
                    Console.WriteLine("End-------------------");
                    while (true)
                    {
                        Console.WriteLine("If want to continue throwing press a positive number, if you want to quit press zero.");
                        string answer = Console.ReadLine();
                        int myNum;
                        if (int.TryParse(answer, out myNum))
                        {
                            if (myNum > 0)
                                break;
                            else
                            {
                                bQuit = true;
                                break;
                            }
                        }
                        else
                            Console.WriteLine("This was not a number, repeat it!");
                    }
                }
            }
        }

        private static string GetAttempt(int number)
        {
            var work = number.ToString();
            if (number == 11 || number == 12 || number == 13)
                return work + "th";
            switch (number % 10)
            {
                case 1: work += "st"; break;
                case 2: work += "nd"; break;
                case 3: work += "rd"; break;
                default: work += "th"; break;
            }
            return work;
        }

        private static double CalculateAverage(List<int> list)
        {
            return Math.Round(list.Average(), 2);
        }
    }

Thanks for your response, this is only my forth week doing programming in my life the answer you gave me is far to detailed as it contains stuff i never seen sorry.

:)
But there is all your were wanted. Why you ask question if you dont know how to make them work then?
I have only provided you a code that you required as said.
At least you can try it out; simply create a new Console project and copy/paste this code inside of it.
Then go line by line through the code to check whats going on. Its not really that complicated as it looks, truly.

I take it you are at Abertay, man. I'm currently trying to do the same thing... I'm reasonably new to computer programming so I don't really know enough to get my head around it, atm.

Check my code is much easier...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int quit=1, rolls=0, won=0, number;
            Random rndm = new Random();

            while (quit > 0)
            {
                Console.Write("Rolling the dice!\nYou throw: ");
                number = rndm.Next(1, 7);
                Console.WriteLine(number);
                if (number == 6)
                {
                    won++; //or won = won + 1; or won+=1;
                    Console.WriteLine("Congratulations, you won!");
                }
                Console.WriteLine("Write positive number to continue throwing dice \nor negative to stop and see the result.");
                quit = int.Parse(Console.ReadLine());
                rolls++; // or rolls = rolls + 1; or rolls+=1;
                Console.WriteLine(); //Just to leave new line
            }
            Console.WriteLine("Average rolls " + (rolls / won) + " needed based on the " + won + " sixes.");
        }
    }
}

as a newbie also i dont get some things here as the c# malrky seems quite tough
whats all the guff about "using" mean in lines 1-4 why do you need it
line 10 method looks like some sort of klingon threat as he is about to attack - what does it mean ?
does this programme just go on for ever if you dont input a negative
why do you have to define a class and a method
much obliged for any one a good c# brain who can also articulate in a newbie way
Lastly why ae there no girls in IT why is it all blokes-no serious any help appreciated

Using is a "shortcut". I just started learning C# about 3 months ago and picking up on it was easy but I have prior experience in other things. You use using to let the compiler know what class you are using so if you had using System you can just type out Console.WriteLine("Yay"); but if you did not you will need to add System in front of it. System.Console.WriteLine("Yay");

thanks cfwebdeveloper
what about the other questions(excluding the one about lack of girls in IT)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.