每次我按下玩家选择的数字时,它都会重复"computer chose __"并且不显示结果。我在这里做错了什么?



/* 简单的石头剪刀布游戏,询问用户是否想玩, 显示计算机选择,并显示结果(如法官),否 先进的技术。如果用户说,我还需要游戏再次循环 是的,可以再玩一次。总的来说,我对编程非常陌生,只是 很难确定基础知识。

using System;
class RockPaperScissors
{
public static void Main(string[] args)
{
Console.Write("Do you want to play rock, paper, scissors? ");
string playerChoice = Console.ReadLine();
playerChoice = playerChoice.ToUpper();

Random r = new Random();
int computerChoice = r.Next(1, 4);
do
{
if (computerChoice == 1)
{
Console.WriteLine("Computer chose Rock");
Console.Write("Player choice: type 1, 2, or 3 (1=rock 2=paper 
3=scissors): ");
Console.ReadKey();
//beginning of switch
switch (playerChoice)
{
case "1":
Console.WriteLine("/nIt is a tie!");
break;
case "2":
Console.WriteLine("/nYou win! Paper covers rock!");
break;
case "3":
Console.WriteLine("/nComputer wins! Rock crushes 
scissors!");
break;
}//end of switch
}
else if (computerChoice == 2)
{
Console.WriteLine("Computer chose paper");
Console.Write("Player choice: type 1, 2, or 3 (1=rock 2=paper 
3=scissors): ");
Console.ReadKey();
//beginning of switch
switch (playerChoice)
{
case "1":
Console.WriteLine("/nComputer wins! Paper covers 
rock!");
break;
case "2":
Console.WriteLine("/nIt is a tie!");
break;
case "3":
Console.WriteLine("/nYou win! Scissors cuts paper!");
break;
} //end of switch
}
else if (computerChoice == 3)
{
Console.WriteLine("Computer chose scissors");
Console.Write("Player choice: type 1, 2, or 3 (1=rock 2=paper 
3=scissors): ");
Console.ReadKey();
//beginning of switch
switch (playerChoice)
{
case "1":
Console.WriteLine("/nYou win! Rock crushes scissors!");
break;
case "2":
Console.WriteLine("/nComputer wins! Scissors cuts 
paper!");
break;
case "3":
Console.WriteLine("/nIt is a tie!");
break;
} //end of switch
}
} while (playerChoice == "YES");
}
}

这是使其循环的一种方法:

string playerChoice = "";
Console.Write("Do you want to play rock, paper, scissors? ");
playerChoice = Console.ReadLine();
while(playerChoice.ToUpper().Trim() == "YES")
{
Random r = new Random();
int computerChoice = r.Next(1, 4);
// Put the rest of the code to figure results of game here

Console.Write("Do you want to play rock, paper, scissors? ");
playerChoice = Console.ReadLine();
} 

您的程序存在一些问题,我将尝试与您一起解决。

  1. 你问玩家是否想玩,如果玩家输入"是",你进入一个do/while循环。 您的程序将永远停留在do/while循环中,因为您永远不会问玩家是否想再次播放,因此答案始终是"是"。

  2. 您正在重复使用您的playerChoice变量,无论是玩家是否想玩游戏,以及他们是否在游戏开始后选择纸、石头或剪刀。 这些应该是两个可能应该是不同的变量。

  3. 您永远不会存储玩家选择纸张,石头,剪刀的结果。 这是问题 #2 的组合,因为您将"YES"与 switch 语句中的 1、2 或 3 进行比较。

要解决问题 #1,您应该重新询问玩家是否要在循环逻辑结束时再次播放,以便从用户那里获得新的是/否输入。

Console.Write("Do you want to play rock, paper, scissors? ");
playerChoice = Console.ReadLine().ToUpper();
} while (playerChoice == "YES");
}
}

要解决问题 #2 和 #3,您应该使用一个新变量来保存用户选择的纸张、石头、剪刀。

Console.Write("Do you want to play rock, paper, scissors? ");
string playerChoice = Console.ReadLine();
playerChoice = playerChoice.ToUpper();

Random r = new Random();
int computerChoice = r.Next(1, 4);
//new variable for holding the player's input for rock/paper/scissor
string playerThrowChoice;
do
{
if (computerChoice == 1)
{
Console.WriteLine("Computer chose Rock");
Console.Write("Player choice: type 1, 2, or 3 (1=rock 2=paper 3=scissors): ");
playerThrowChoice = Console.ReadKey();
//beginning of switch
//notice we are using our new variable here
switch (playerThrowChoice )
{
case "1":
Console.WriteLine("/nIt is a tie!");
break;
case "2":
Console.WriteLine("/nYou win! Paper covers rock!");
break;
case "3":
Console.WriteLine("/nComputer wins! Rock crushes scissors!");
break;
}//end of switch
}
/// ....the rest of your logic

最新更新