使用随机枚举

时间:2015-06-24 04:51:42

标签: c# enumeration

您好我正在制作一个简单的猎枪游戏,其中用户与计算机并选择拍摄,屏蔽或重新加载但是当我尝试将枚举设置为随机时它会给我错误

  

无法将int类型隐式转换为ShotgunGame.Program.ShotgunOption。存在显式转换(您是否错过了演员?),

我不知道如何解决这个问题。

任何指导都将不胜感激

//Declare Variables
        Console.Title = "Welcome To The Shotgune Game";
        int CPUBullets = 3, userBullets = 3;
        ShotgunOption UserOption;
        int computerChoice, userScore = 0;
        bool QUIT = false;
        double gameCount = 0.0;
        Random computer = new Random();

        Console.Clear();
        Console.WriteLine("SHOOT RELOAD SHIELD");
         do
        {
            do
            {
            //Console.Write("Please enter choice, or enter QUIT to quit: ");
            UserOption = GetOptionFromUser();

            if (UserOption.ToUpper() == "QUIT")
            {
                break;
            }
            ShotgunOption CPUOption = computer.Next(1, 3); // 1 is Shot, 2 is Reload, 3 is Shield



              switch (UserOption.ToUpper())
            {
                case "SHOOT":
                    if (computerChoice == 1)
                    {
                        Console.WriteLine("You chose {0} and the computer chose Shoot. It was a tie!", userChoice);
                        ; userBullets --;CPUBullets --; ++gameCount;
                    }
                    else if (computerChoice == 2)
                    {
                        Console.WriteLine("You chose {0} and the computer chose Reload. You win!", userChoice);
                        ++userScore; ++gameCount;
                    }
                    else if (computerChoice == 3)
                    {
                        Console.WriteLine("You chose {0} and the computer chose Shield. No Damage!", userChoice);
                        ++gameCount;
                    }
                    break;
                case "RELAOD":
                    if (computerChoice == 1)
                    {
                        Console.WriteLine("You chose {0} and the computer chose Shoot. You lose!", userChoice);
                         ++userScore; ++gameCount;
                    }
                    else if (computerChoice == 2)
                    {
                        Console.WriteLine("You chose {0} and the computer chose Reload. You Both Gain A bullet", userChoice);
                        userBullets++; CPUBullets++; ++gameCount;
                    }
                    else if (computerChoice == 3)
                    {
                        Console.WriteLine("You chose {0} and the computer chose Shield. No Damage!", userChoice);

                    }
                    break;
                case "SHIELD":
                    if (computerChoice == 1)
                    {
                        Console.WriteLine("You chose {0} and the computer chose Shoot. You lose!", userChoice);
                        ++gameCount;
                    }
                    else if (computerChoice == 2)
                    {
                        Console.WriteLine("You chose {0} and the computer chose Reload. You win!", userChoice);
                        ++userScore; ++gameCount;
                    }
                    else if (computerChoice == 3)
                    {
                        Console.WriteLine("You chose {0} and the computer chose Shield. No Damage!", userChoice);
                        ++gameCount;
                    }
                    break;                  

            }
          }
          while (UserOption != ShotgunOption.Shield || CPUOption != 4);
        } while (QUIT == false || gameCount == 3);

2 个答案:

答案 0 :(得分:1)

  

Shotgune游戏

正如错误消息所示,您正在尝试将int分配给期望枚举值的变量。但是,您可以将int转换为枚举:

(EnumName)integerValue

所以在这种情况下:

ShotgunOption CPUOption = (ShotgunOption)computer.Next(1, 3);

答案 1 :(得分:0)

Array values = Enum.GetValues(typeof(ShotgunOption));
Random computer = new Random();
ShotgunOption CPUOption = (ShotgunOption)values.GetValue(computer.Next(values.Length));

试试这个......