使用随机枚举
Using Random With Enumeration
你好,我正在制作一个简单的霰弹枪游戏,用户可以在其中选择射击、屏蔽或装弹但是当我尝试将枚举设置为随机时,出现错误
Cannot implicitly convert type int
to ShotgunGame.Program.ShotgunOption
. An explicit conversion exists (are you missing a cast?),
我不知道如何解决这个问题。
任何指导将不胜感激
//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);
Shotgune game
如错误消息所示,您正在尝试将 int 分配给需要枚举值的变量。不过,您可以将 int 转换为枚举:
(EnumName)integerValue
所以在这种情况下:
ShotgunOption CPUOption = (ShotgunOption)computer.Next(1, 3);
Array values = Enum.GetValues(typeof(ShotgunOption));
Random computer = new Random();
ShotgunOption CPUOption = (ShotgunOption)values.GetValue(computer.Next(values.Length));
试试这个...
你好,我正在制作一个简单的霰弹枪游戏,用户可以在其中选择射击、屏蔽或装弹但是当我尝试将枚举设置为随机时,出现错误
Cannot implicitly convert type
int
toShotgunGame.Program.ShotgunOption
. An explicit conversion exists (are you missing a cast?),
我不知道如何解决这个问题。
任何指导将不胜感激
//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);
Shotgune game
如错误消息所示,您正在尝试将 int 分配给需要枚举值的变量。不过,您可以将 int 转换为枚举:
(EnumName)integerValue
所以在这种情况下:
ShotgunOption CPUOption = (ShotgunOption)computer.Next(1, 3);
Array values = Enum.GetValues(typeof(ShotgunOption));
Random computer = new Random();
ShotgunOption CPUOption = (ShotgunOption)values.GetValue(computer.Next(values.Length));
试试这个...