我是C#的新手,我正在编写一个简单的Rock,Paper Scissors游戏,但是我的“switch”命令有问题。 我希望用户输入Rock,Paper或Scissors,然后输入输入并转到特定情况,但我无法弄清楚如何操作。
{
//random number generator
Random random = new Random();
int randomNumber = random.Next(1,4);
//user input
Console.WriteLine("PLease Enter Rock, Paper or Scissors");
string input = Console.ReadLine();
//compare input and random number
switch (input)
{
case Rock:
if (randomNumber == 1)
{
Console.WriteLine("It's a draw, computer also chose Rock");
}
else if (randomNumber == 2)
{
Console.WriteLine("You loose, computer chose Paper");
}
else if (randomNumber == 3)
{
Console.WriteLine("You win, computer chose Scissors");
}
break;
case Paper:
if (randomNumber == 1)
{
Console.WriteLine("You win, computer chose Rock");
}
else if (randomNumber == 2)
{
Console.WriteLine("It's a draw, computer also chose Paper");
}
else if (randomNumber == 3)
{
Console.WriteLine("You loose, computer chose Scissors");
}
break;
case Scissors:
if (randomNumber == 1)
{
Console.WriteLine("You loose, computer chose Rock");
}
else if (randomNumber == 2)
{
Console.WriteLine("You win, computer chose Paper");
}
else if (randomNumber == 3)
{
Console.WriteLine("It's a draw, computer chose Scissors");
}
break;
}
答案 0 :(得分:2)
案例必须写成字符串文字(引号):
switch (input)
{
case "Rock":
// ...
break;
case "Paper":
// ...
break;
case "Scissors":
// ...
break;
}
或者,定义一些包含这些值的变量,例如
string Rock = "Rock";
答案 1 :(得分:0)
您必须将案例写为字符串:
...
case "Rock":
...