我需要让剪刀赢得纸张,摇滚胜利剪刀等等。我目前有声明userChoice!= computerChoice但这不会起作用,因为摇滚会赢得纸张。我不知道如何做这项工作,我已经尝试过思考,目前无法思考任何事情,我只是一个初学程序员。
const int ROCK = 1;
const int PAPER = 2;
const int SCISSORS = 3;
private void rockButton_Click(object sender, EventArgs e)
{
int userChoice = ROCK;
userPictureBox.Image = Properties.Resources.Rock;
Random randomNumberGenerator = new Random();
int computerChoice = randomNumberGenerator.Next(1, 4);
switch (computerChoice)
{
case ROCK:
computerPictureBox.Image = Properties.Resources.Rock;
break;
case PAPER:
computerPictureBox.Image = Properties.Resources.Paper;
break;
case SCISSORS:
computerPictureBox.Image = Properties.Resources.Scissors;
break;
}
if (userChoice == computerChoice)
MessageBox.Show("It's a tie.");
else if (userChoice != computerChoice)
{
MessageBox.Show("You win!");
}
}
private void paperButton_Click(object sender, EventArgs e)
{
int userChoice = PAPER;
userPictureBox.Image = Properties.Resources.Paper;
Random randomNumberGenerator = new Random();
int computerChoice = randomNumberGenerator.Next(1, 4);
switch (computerChoice)
{
case ROCK:
computerPictureBox.Image = Properties.Resources.Rock;
break;
case PAPER:
computerPictureBox.Image = Properties.Resources.Paper;
break;
case SCISSORS:
computerPictureBox.Image = Properties.Resources.Scissors;
break;
}
if (userChoice == computerChoice)
MessageBox.Show("It's a tie.");
else if (userChoice != computerChoice)
{
MessageBox.Show("You win!");
}
}
private void scissorsButton_Click(object sender, EventArgs e)
{
int userChoice = SCISSORS;
userPictureBox.Image = Properties.Resources.Scissors;
Random randomNumberGenerator = new Random();
int computerChoice = randomNumberGenerator.Next(1, 4);
switch (computerChoice)
{
case ROCK:
computerPictureBox.Image = Properties.Resources.Rock;
break;
case PAPER:
computerPictureBox.Image = Properties.Resources.Paper;
break;
case SCISSORS:
computerPictureBox.Image = Properties.Resources.Scissors;
break;
}
if (userChoice == computerChoice)
MessageBox.Show("It's a tie.");
else if (userChoice != computerChoice)
{
MessageBox.Show("You win!");
答案 0 :(得分:0)
创建矩阵dict
,其中行 - 人类选择,列 - 计算机选择和dict[i,j]
- 结果,胜者:人或计算机。
var dict = new Dictionary<int, Dictionary<int, string>>();
dict[ROCK] = new Dictionary<int, string> { { ROCK , "draw"}, {PAPER, "computer"}, {SCISSORS , "human"} };
dict[PAPER] = new Dictionary<int, string> { { ROCK , "human" }, { PAPER, "draw" }, { SCISSORS, "computer" } };
dict[SCISSORS] = new Dictionary<int, string> { { ROCK , "computer" }, { PAPER , "human" }, { SCISSORS , "draw" } };
var winner = dict[userChoice, computerChoice];
答案 1 :(得分:0)
if(userChoice == 1 && computerChoice == 2)
{
MessageBox.Show("You win!")
}
else if(userChoice == 2 && computerChoice == 3)
{
MessageBox.Show("You lose!")
}
else if(userChoice == 3 && computerChoice == 1)
{
MessageBox.Show("You lose!")
}
因为你有摇滚,纸和剪刀的价值。
我的同事告诉我这个,看起来很基本但有逻辑。注意:只需添加其他案例。