none
Dice roll game RRS feed

  • Question

  • Hi!

    I am trying to create a dice roll simulator.

    With the code below how do I:

    1. If the user rolls a 6 that number is not added to the sum but they are allowed to roll twice, adding the sum of those die to the total sum.

    2. At the end, print the number of rolls and total sum of all the rolls

    using System;

    namespace EnLoop
    {
        class MainClass
        {
            public static void Main(string[] args)
            {
                Console.WriteLine("How many die would you like to throw?");
                string numberofThrowsstring = Console.ReadLine();
                Console.WriteLine("Nice, that makes " + 
    numberofThrowsstring  +" throws.");

    Console.ReadKey();

    int numberofThrows int.Parse(
    numberofThrowsstring);                                                                                                                                                                                                                                                                                                                                                                                                                               );

    Random rnd newRandom();

    int sum=0;

    while(
    numberofThrows 0)
    {

    int randomRoll rnd.Next(1,7);
    if (randomRoll == 6)

    {
    Console.WriteLine("You rolled a 6, you get to roll the die two times.");
    }

    else
    {
    sum sum randomRoll;
    Console.WriteLine("You have rolled "+randomRoll+" and the sum is now: "+sum);
    }

    numberofThrows numberofThrows 1;
    }

    Console.WriteLine(
    numberofThrows);
    }
    }
    }

    Saturday, May 18, 2019 8:04 PM

All replies

  • Unless I'm missing the point here, the net effect of rolling a 6 is that you get 2 extra throws.  So, just add 2 to numberofThrows.

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Sunday, May 19, 2019 5:03 AM
  • Hi Manda_555,

    Thank you for posting here.

    For your question, the code you provided confused me. How do you get the number? Input it by yourself or generate it randomly? Why do you use both of them?

    I modify the code via generating random number. I use for loop to generate the number in 100 times.

      public static void Main(string[] args)
            {
                int sum = 0;
                Random rnd = new Random();
                for (int i = 0; i < 100; i++)
                {
                Continue:
                    Console.WriteLine("Roll the die");
                    //Console.WriteLine("Nice, that makes " + numberofThrowsstring + " throws.");
    
    
                    int randomRoll = rnd.Next(1, 7);
                    if (randomRoll == 6)
                    {
                        Console.WriteLine("You rolled a 6, you get to roll the die two times.");
                        goto Continue;
                    }
                    else
                    {
                        sum = sum + randomRoll;
                        Console.WriteLine("You have rolled " + randomRoll + " and the sum is now: " + sum);
                    }
                    i++;
                }
    
    
                Console.ReadKey();
            }

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by zarahhhh Thursday, May 23, 2019 9:19 AM
    Monday, May 20, 2019 7:23 AM
    Moderator