samedi 23 juillet 2016

How can I make a running total that keeps its value outside of the switch case?

I am writing my first program without the use of a tutorial. It is a polyhedral die rolling program in C#. The user can input the type of dice to be rolled and the number of dice. It is working well except that I'm not sure how to add up the total and then print it out. It is successfully printing each individual roll, though, so I know it is working as far as generating the random numbers in the proper range determined by the user. I wanted to print the total at the end of the program, but it is not working. Does the value of the variable disappear after the program leaves the switch case? Is there something I can do to fix that? Thanks.

    static void Main(string[] args)
    {
        Random roll = new Random();

        // Request dice type from user
        Console.WriteLine("Please input the type of dice you want to roll. ");

        // Display dice types to user
        Console.WriteLine("4) Four-sided");
        Console.WriteLine("6) Six-sided");
        Console.WriteLine("8) Eight-sided");
        Console.WriteLine("10) Ten-sided");
        Console.WriteLine("12) Twelve-sided");
        Console.WriteLine("20) Twenty-sided");
        Console.WriteLine("100) Percentage");
        Console.WriteLine(" ");

        // Take dice type from user
        int typeOfDice = Convert.ToInt32(Console.ReadLine());

        Random rnd = new Random();

        // Request number of dice from user
        Console.WriteLine("Please input the number of dice you want to roll ");
        Console.WriteLine(" ");

        // Accept number of dice from user 
        int numberOfDice = Convert.ToInt32(Console.ReadLine());


        // Assigns random generator parameters to user's choice of dice type
        switch (typeOfDice)
        {

            case 4:
                for(int count = 0; count < numberOfDice; count++)
                {

                    int currentRoll = rnd.Next(typeOfDice);
                    int total = currentRoll + 1;
                    Console.WriteLine(currentRoll + 1);
                    Console.WriteLine(" ");

                }
                break;

            case 6:
                for (int count = 0; count < numberOfDice; count++)
                {

                    int currentRoll = rnd.Next(typeOfDice);
                    int total = currentRoll + 1;
                    Console.WriteLine(currentRoll + 1);
                    Console.WriteLine(" ");

                }
                break;

            case 8:
                for (int count = 0; count < numberOfDice; count++)
                {

                    int currentRoll = rnd.Next(typeOfDice);
                    int total = currentRoll + 1;
                    Console.WriteLine(currentRoll + 1);
                    Console.WriteLine(" ");

                }
                break;

            case 10:
                for (int count = 0; count < numberOfDice; count++)
                {

                    int currentRoll = rnd.Next(typeOfDice);
                    int total = currentRoll + 1;
                    Console.WriteLine(currentRoll + 1);
                    Console.WriteLine(" ");

                }
                break;

            case 12:
                for (int count = 0; count < numberOfDice; count++)
                {

                    int currentRoll = rnd.Next(typeOfDice);
                    int total = currentRoll + 1;
                    Console.WriteLine(currentRoll + 1);
                    Console.WriteLine(" ");

                }
                break;

            case 20:
                for (int count = 0; count < numberOfDice; count++)
                {

                    int currentRoll = rnd.Next(typeOfDice);
                    int total = currentRoll + 1;
                    Console.WriteLine(currentRoll + 1);
                    Console.WriteLine(" ");

                }
                break;

            case 100:
                for (int count = 0; count < numberOfDice; count++)
                {

                    int currentRoll = rnd.Next(typeOfDice);
                    int total = currentRoll + 1;
                    Console.WriteLine(currentRoll + 1);
                    Console.WriteLine(" ");

                }
                break;

        }

       // Console.WriteLine(total);

        // int[] xfiles = new int[amountofdice];

    }




Aucun commentaire:

Enregistrer un commentaire