我知道这个问题之前已经发布了,我现在正在使用一些给我的代码但是在添加了一些东西后,我遇到了让它实际输出我想要的问题。
这就是我的控制台上出现的内容
我在做什么不会导致程序进入游戏?
using System;
class rockpsV2
{
public static void Main(string[]args)
{
do
{
Console.WriteLine("Do you want to play rock,paper or scissors?");
string userChoice = Console.ReadLine();
Random r = new Random();
int computerChoice = r.Next(4);
if (computerChoice == 1)
{
if (userChoice == "rock")
{
Console.WriteLine("The computer chose rock");
Console.WriteLine("It is a tie ");
}
else if (userChoice == "paper")
{
Console.WriteLine("The computer chose paper");
Console.WriteLine("It is a tie ");
}
else if (userChoice == "scissors")
{
Console.WriteLine("The computer chose scissors");
Console.WriteLine("It is a tie ");
}
else
{
Console.WriteLine("You must choose rock,paper or scissors!");
}
}
else if (computerChoice == 2)
{
if (userChoice == "rock")
{
Console.WriteLine("The computer chose paper");
Console.WriteLine("Sorry you lose,paper beat rock");
}
else if (userChoice == "paper")
{
Console.WriteLine("The computer chose scissors");
Console.WriteLine("Sorry you lose,scissors beat paper ");
}
else if (userChoice == "scissors")
{
Console.WriteLine("The computer chose rock");
Console.WriteLine("Sorry you lose,rock beats scissors");
}
else
{
Console.WriteLine("You must choose rock,paper or scissors!");
}
}
else if (computerChoice == 3)
{
if (userChoice == "rock")
{
Console.WriteLine("The computer chose scissors");
Console.WriteLine("You win,rock beats scissors");
}
else if (userChoice == "paper")
{
Console.WriteLine("The computer chose rock");
Console.WriteLine("You win,paper beats rock");
}
else if (userChoice == "scissors")
{
Console.WriteLine("The computer chose paper");
Console.WriteLine("You win,scissors beat paper");
}
else
{
Console.WriteLine("You must choose rock,paper or scissors!");
}
}
} while(Console.ReadLine() == "yes");
}
}
答案 0 :(得分:2)
问题是:Console.WriteLine("Do you want to play rock,paper or scissors?");
(请注意单词'或',您在问题中将其写为'以及')您使用摇滚,纸张或其他方式回答该问题剪刀,如果你给出一个不同的答案,它就不会播放并进入while(Console.ReadLine() == "yes");
线,如果你回答的不同于“是”和“#39;在这里,它将离开do..while循环并退出程序
答案 1 :(得分:0)
我会以这种方式重构整个程序
using System;
class rockpsV2
{
public static string[] choices = new string[] { "rock", "paper", "scissor" };
public static int comparePlay(string player1, string player2)
{
if (player1 == player2)
return 0;
if (player1 == "rock" && player2 == "scissor" ||
player1 == "paper" && player2 == "rock" ||
player1 == "scissor" && player2 == "paper")
return -1;
return 1;
}
public static bool validChoice(string choice)
{
return choice == "rock"
|| choice == "scissor"
|| choice == "paper";
}
public static void Main(string[] args)
{
string userChoice = "";
do
{
do {
Console.WriteLine("Do you want to play rock,paper or scissors?");
userChoice = Console.ReadLine();
if (!validChoice(userChoice)) {
Console.WriteLine("You must choose rock,paper or scissors!");
}
}
while(!validChoice(userChoice));
Random r = new Random();
int computerChoiceRand = r.Next(100) % 3;
string computerChoice = choices[computerChoiceRand];
Console.WriteLine("The computer chose {0}", computerChoice);
int whoWins = comparePlay(computerChoice, userChoice);
if (whoWins < 0) {
Console.WriteLine("Sorry you lose, {0} beats {1}", computerChoice, userChoice);
}
if (whoWins == 0) {
Console.WriteLine("It is a tie");
}
if (whoWins > 0) {
Console.WriteLine("You win,{0} beats {1}", userChoice, computerChoice);
}
Console.WriteLine("Want to play again?");
} while(Console.ReadLine() == "yes");
}
}