我的任务是编写程序,以便在玩家2需要输入他们的选择之前显示计算机的随机选择。我无法将计算机的随机选择链接到我的if语句。到目前为止,这是我的代码:
public static void Main(string[] args) {
Random random = new Random();
int computer = random.Next(1, 4);
int player2 = 0;
string response;
string willYouPlayAgian;
Console.Write("Do you want to play Rock, Paper, Scissors? ");
willYouPlayAgian = Console.ReadLine();
willYouPlayAgian = willYouPlayAgian.ToUpper();
while (willYouPlayAgian == "YES") {
if (computer == 1)
{
Console.WriteLine("Player 1 <Compter> selection - Rock" + "\n");
}
if (computer == 2)
{
Console.WriteLine("\nPlayer 1 <Compter> selection - Paper" + "\n");
}
if (computer == 3)
{
Console.WriteLine("\nPlayer 1 <Compter> selection - Scissors" + "\n");
}
Console.Write("Player 2 selection (1=Rock, 2=Paper, 3=Scissors): ");
response = Console.ReadLine();
player2 = int.Parse(response);
if (computer == player2)
Console.WriteLine("\nDraw. No winner.");
else
if (computer == 1 && player2 == 2)
Console.WriteLine("\nPaper smothers Rock. Player 2 wins!!!");
else
if (computer == 1 && player2 == 3)
Console.WriteLine("\nRock destroys Scissors. Player 1 wins!!!");
else
if (computer == 2 && player2 == 1)
Console.WriteLine("\nPaper smothers Rock. Player 1 wins!!!");
else
if (computer == 2 && player2 == 3)
Console.WriteLine("\nScissors slices Paper. Player 2 Wins!!!");
else
if (computer == 3 && player2 == 1)
Console.WriteLine("\nRock destroys Scissors. Player 2 wins!!!");
else
if (computer == 3 && player2 == 2)
Console.WriteLine("\nScissors slices Paper. Player 1 wins!!!");
Console.Write("\nDo you want to play Rock, Paper, Scissors? ");
willYouPlayAgian = Console.ReadLine();
willYouPlayAgian = willYouPlayAgian.ToUpper();
}
答案 0 :(得分:1)
将此行向下移动到while
循环:
int computer = random.Next(1, 4);
像这样:
while (willYouPlayAgian == "YES")
{
int computer = random.Next(1, 4);
if (computer == 1)
{