您好我正在尝试制作一个简单的猎枪游戏,其中用户与CPU以及选择镜头,屏蔽或重新加载,但在我的switch语句和我的if语句中,当我尝试使用UserOption时,它给了我错误
运营商
==
无法应用于ShotgunGame.Program.ShotgunOption
和int
类型的操作数。
我不知道如何解决这个问题。
任何指导都将不胜感激
//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");
UserOption = GetOptionFromUser();
ShotgunOption CPUOption = (ShotgunOption)computer.Next(1, 3); // 1 is Shot, 2 is Reload, 3 is Shield
do
{
if (UserOption == "QUIT")
{
break;
}
do
{
//Console.Write("Please enter choice, or enter QUIT to quit: ");
switch (UserOption)
{
case "SHOOT":
if (CPUOption == 1)
{
Console.WriteLine("You chose {0} and the computer chose Shoot. It was a tie!", userChoice);
; userBullets --;CPUBullets --; ++gameCount;
}
else if (CPUOption == 2)
{
Console.WriteLine("You chose {0} and the computer chose Reload. You win!", userChoice);
++userScore; ++gameCount;
}
else if (CPUOption == 3)
{
Console.WriteLine("You chose {0} and the computer chose Shield. No Damage!", userChoice);
++gameCount;
}
break;
case "RELAOD":
if (CPUOption == 1)
{
Console.WriteLine("You chose {0} and the computer chose Shoot. You lose!", userChoice);
++userScore; ++gameCount;
}
else if (CPUOption == 2)
{
Console.WriteLine("You chose {0} and the computer chose Reload. You Both Gain A bullet", userChoice);
userBullets++; CPUBullets++; ++gameCount;
}
else if (CPUOption == 3)
{
Console.WriteLine("You chose {0} and the computer chose Shield. No Damage!", userChoice);
}
break;
case "SHIELD":
if (CPUOption == 1)
{
Console.WriteLine("You chose {0} and the computer chose Shoot. You lose!", userChoice);
++gameCount;
}
else if (CPUOption == 2)
{
Console.WriteLine("You chose {0} and the computer chose Reload. You win!", userChoice);
++userScore; ++gameCount;
}
else if (CPUOption == 3)
{
Console.WriteLine("You chose {0} and the computer chose Shield. No Damage!", userChoice);
++gameCount;
}
break;
}
}
while (UserOption != ShotgunOption.Shield || CPUOption != ShotgunOption.Shield);
} while (QUIT == false || gameCount == 3);
继承我的ShotgunOption枚举方法
enum ShotgunOption
{
Shoot = 1,
Reload = 2,
Shield = 3,
}
答案 0 :(得分:5)
您需要使用显式转换,因为ShotgunOption是枚举。
if((int)CPUOption == 1)
{
...
}
您已使用以下代码将int
转换为enum
类型,因此应明确您需要采用反向过程将枚举类型与int进行比较
ShotgunOption CPUOption = (ShotgunOption)computer.Next(1, 3);