/*简单的石头剪刀游戏,询问用户是否想玩,显示计算机选择,并显示结果(像法官一样),没有高级技术。如果用户同意再次玩游戏,我还需要让游戏再次循环。一般来说,我是一个非常非常新的编程新手,只是很难掌握基础知识。*/
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");
}
}
发布于 2017-04-13 22:33:47
这是让它循环的一种方法:
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();
}
发布于 2017-04-14 18:18:26
你的程序中有几个问题,我会试着和你一起解决。
playerChoice
变量,无论是玩家想玩游戏还是游戏开始后他们选择了布、石头或剪刀。这应该是两个应该是不同的变量。要解决问题#1,您应该重新确定玩家是否希望在循环逻辑结束时再次玩游戏,以便从用户获得新的YES/NO输入。
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
https://stackoverflow.com/questions/43402295
复制