我随机挑选的电脑总是剪刀
本文关键字:电脑 随机 挑选 | 更新日期: 2023-09-27 18:26:39
我的任务是以这样一种方式编写程序,即在玩家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();
}
将此行向下移动到while
循环:
int computer = random.Next(1, 4);
像这样:
while (willYouPlayAgian == "YES")
{
int computer = random.Next(1, 4);
if (computer == 1)
{