我正在做一個岩石,剪刀紙遊戲窗體應用程序和代碼通過,但我似乎無法讓它的工作,因爲它應該。用戶將輸入紙張,剪刀或搖滾,然後計算機將選擇1-3,1是岩石,2是紙張,3是剪刀。你會贏,輸或者是配合。有時候沒有印刷品,否則它說你贏了,但是你真的輸了。如果代碼難以理解,請原諒我。我是一名初學者。岩石紙剪刀遊戲與錯誤的結果
string userAnswer = textBox1.Text;
Random r1 = new Random();
int b = 3;
string comrock = "The computer chose rock!";
string compaper = "The computer chose paper!";
string comscissors = "The computer chose scissors!";
int computerChoice = r1.Next(b);
if (computerChoice == 1)
{
label4.Text = comrock;
}
else if (computerChoice == 2)
{
label4.Text = compaper;
}
else if (computerChoice == 3)
{
label4.Text = comscissors;
}
else
{
while (computerChoice == 0)
{
computerChoice = r1.Next(b);
if (computerChoice == 1)
{
label4.Text = comrock;
}
else if (computerChoice == 2)
{
label4.Text = compaper;
}
else if (computerChoice == 3)
{
label4.Text = comscissors;
}
}
if (userAnswer == "rock") ;
{
if (computerChoice == 2) ;
{
label3.Text = "You lost....";
}
}
if (userAnswer == "paper") ;
{
if (computerChoice == 1) ;
{
label3.Text = "You Won!";
}
}
if (userAnswer == "rock") ;
{
if (computerChoice == 1) ;
{
label3.Text = "It's a tie!";
}
}
if (userAnswer == "rock") ;
{
if (computerChoice == 3) ;
{
label3.Text = "You lost....";
}
}
if (userAnswer == "paper");
{
if (computerChoice == 2) ;
{
label3.Text = "It's a tie!";
}
}
if (userAnswer == "paper");
{
if (computerChoice == 3) ;
{
label3.Text = "You lost....";
}
}
if (userAnswer == "scissors");
{
if (computerChoice == 2) ;
{
label3.Text = "You won!";
}
}
if (userAnswer == "scissors");
{
if (computerChoice == 3) ;
{
label3.Text = "It's a tie!";
}
}
if (userAnswer == "scissors");
{
if (computerChoice == 2) ;
{
label3.Text = "You won!";
}
}
這大約是最面向對象的C#可以想見。此外,RPS邏輯與UI邏輯的混合對於初學者愛好者來說是很好的,但對於維護或團隊設置來說並不好。 –
我不想想你的代碼,如果它與玩 - 十個不同的*事* –
我已經在這裏都已經回答了這個問題http://stackoverflow.com/questions/38965901/how-to-display - 結果 - 岩石剪刀 - 遊戲/ 38966414#38966414 –