如何从操作中排除多个值?我会在使用if-else语句的同时使用or运算符吗

本文关键字:语句 if-else 运算符 or 操作 排除 | 更新日期: 2023-09-27 17:58:30

我知道如果我只使用连续的数字会更容易,但我想让用户更容易选择与他们选择的模具类型相对应的数字。如果我使用or运算符,我是否仅限于比较两件事?这是我想做的,但没有成功。我的语法有错吗?或者我不能在一个语句中字符串显示多个术语吗?:

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

这是我试图让它工作的短程序。我只想确保用户不会破坏程序:

    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;
        }
    }

如何从操作中排除多个值?我会在使用if-else语句的同时使用or运算符吗

您想要一个&amp;(逻辑与)在您的测试之间,而不是||(逻辑或)

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

对于|| operator,如果您为骰子选择了正确的值,则if求值始终为true
例如,假设你的用户选择了一个骰子=4,现在这个骰子值(4)不同于骰子值6(或其他可接受的值),因此你的条件总是会找到一个真实的条件,并且你的代码会打印错误消息

此外,我希望向您介绍enum关键字。

public enum DiceType
{
   FourSides = 4,
   SixSides= 6,
   EightSides = 8,
   TenSides = 10,
   TwelveSides = 12,
   TwentySides = 20,
   Percentage = 100
}

现在,您不用幻数,而是使用这种枚举类型的变量

Random roll = new Random();
while(true)
{
    // 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(" ");
    Console.WriteLine("Type quit to exit ");
    // Take dice type from user
    Console.Write("Type of Dice: ");
    string input = Console.ReadLine();
    if(input == "quit")
       break;
    DiceType typeOfDice;
    // Try to parse the input and check if it could be an enum of the 
    // desidered type (Note user can also input "foursides" not just 4
    if (!Enum.TryParse<DiceType>(input, true, out typeOfDice) || 
        !Enum.IsDefined(typeof(DiceType), typeOfDice))
        Console.WriteLine("That is not an acceptable die type. Please try again.");
    else
    {
       ....
       switch (typeOfDice)
       {
           case DiceType.FourSides:
               .....
               break;
           case DiceType.SixSides:
               .....
               break;
           ....
        }
    }
}

我还删除了goto代码。我在代码周围添加了一个无限循环,而不是这种著名的坏做法。菜单中的一个新选项(退出)允许用户退出循环并终止程序