2017-01-08 9 views
0

Я знаю, что этот вопрос был опубликован раньше, я в настоящее время использую код, который мне был дан, но после добавления некоторых вещей у меня возникли проблемы с получением его на самом деле Я хочу.Проблемы с моим камнем, бумажными ножницами

Это то, что приходит на моей консоли

  • Вы хотите, чтобы играть рок, бумагу и ножницы?
  • Да
  • Вы должны выбрать между каменной бумагой и ножницами!
  • рок
  • выходит из программы

Что я делаю это не вызывает программу, чтобы перейти в игру?

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

Пожалуйста, постарайтесь предоставить меньший пример/описать вашу проблему более точно ДЕТАЛЬ – torkleyy

ответ

2

Вопрос заключается в том: Console.WriteLine("Do you want to play rock,paper or scissors?"); (обратите внимание на слово «или», вы написали в своем вопросе, как «и») вы ответить на этот вопрос с рок, бумаги или ножницы, если дать другой ответ, это не играет и идет к while(Console.ReadLine() == "yes"); линии, если вы ответите что-либо иное, чем «да» здесь, он оставит петлю do..while и выйти из программы

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"); 
    } 
} 

 Смежные вопросы

  • Нет связанных вопросов^_^