dimanche 24 juillet 2016

How can I exclude multiple values from an operation? Would I use an or operator along with an if else statement?

I know it would be easier if I just use consecutive numbers, but I want to make it easier for the user to select a number that corresponds to the die type they pick. If I use an or operator am I limited to comparing just two things? This is what I tried to do but it didn't work. Is my syntax wrong or can I not string multiple terms in one statement?:

if (typeOfDice != 4 || typeOfDice != 6 || 
        typeOfDice != 8 || typeOfDice != //10 || typeOfDice != 12 || 
        typeOfDice != 20 || typeOfDice != 100)

Here is the short program I'm trying to make it work in. I just want to make sure the user can't break the program:

    static void Main(string[] args)
    {
        Start:
        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
        Console.Write("Type of Dice: ");
        int typeOfDice = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine(" ");

        // Prevents user from breaking the program by printing a corrective message
        // and restarting the program in the event an inappropriate choice is made by the user.
        if (typeOfDice != 4 || typeOfDice != 6 ||
        typeOfDice != 8 || typeOfDice != //10 || typeOfDice != 12 || 
        typeOfDice != 20 || typeOfDice != 100)
            Console.WriteLine("That is not an acceptable die type. Please try again.");
        goto Start;
        else
        {

            // Initiates random variable and total variable
            Random rnd = new Random();
            int total = 0;

            // 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 
            Console.Write("Number of Dice: ");
            int numberOfDice = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(" ");

            /// Assigns random generator parameters to user's choice of dice type and 
            // generates random number, looping until the die is rolled the requested 
            // number of times and the result of each new roll is added to the total
            switch (typeOfDice)
            {

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

                        int currentRoll = rnd.Next(typeOfDice);
                        total += currentRoll + 1;
                        Console.Write("{0} ", currentRoll + 1);
                        //Console.WriteLine(" ");

                    }
                    break;

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

                        int currentRoll = rnd.Next(typeOfDice);
                        total += currentRoll + 1;
                        Console.Write("{0} ", currentRoll + 1);
                        //Console.WriteLine(" ");

                    }
                    break;

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

                        int currentRoll = rnd.Next(typeOfDice);
                        total += currentRoll + 1;
                        Console.Write("{0} ", currentRoll + 1);
                        //Console.WriteLine(" ");

                    }
                    break;

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

                        int currentRoll = rnd.Next(typeOfDice);
                        total += currentRoll + 1;
                        Console.Write("{0} ", currentRoll + 1);
                        //Console.WriteLine(" ");

                    }
                    break;

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

                        int currentRoll = rnd.Next(typeOfDice);
                        total += currentRoll + 1;
                        Console.Write("{0} ", currentRoll + 1);
                        //Console.WriteLine(" ");

                    }
                    break;

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

                        int currentRoll = rnd.Next(typeOfDice);
                        total += currentRoll + 1;
                        Console.Write("{0} ", currentRoll + 1);
                        // Console.WriteLine(" ");

                    }
                    break;

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

                        int currentRoll = rnd.Next(typeOfDice);
                        total += currentRoll + 1;
                        Console.Write("{0} ", currentRoll + 1);
                        //Console.WriteLine(" ");

                    }
                    break;

            }

            // Prints total of dice rolls.
            Console.WriteLine(" ");
            Console.WriteLine("Total: {0}", total);
            Console.WriteLine(" ");

            goto Start;
        }

    }




Aucun commentaire:

Enregistrer un commentaire