0

連続した数字を使用した方が簡単なのはわかっていますが、ユーザーが選んだサイコロの種類に対応する数字を簡単に選択できるようにしたいと考えています。or 演算子を使用する場合、2 つのものだけを比較することに限定されますか? これは私がやろうとしたことですが、うまくいきませんでした。私の構文は間違っていますか、それとも 1 つのステートメントで複数の用語を文字列にすることはできませんか?:

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

    }
4

1 に答える 1

1

|| ではなく、テスト間に && (論理 AND) が必要です。(論理和)

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

|| operator、サイコロに正しい値を選択した場合も、if 評価は常に true になります。
たとえば、ユーザーがサイコロ = 4 を選択するとします。このサイコロの値 (4) はサイコロの値 6 (またはその他の許容値) とは異なるため、条件は常に true である条件を見つけ、コードはエラーメッセージ

また、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 コードも削除しました。この有名な悪い習慣の代わりに、コードの周りに無限ループを追加しました。メニューの新しい選択肢 (quit) により、ユーザーはループを終了してプログラムを終了できます

于 2016-07-24T19:54:50.547 に答える