2015-03-02 104 views
-3

我正在做一个简单的纸,岩石,剪刀游戏(c#),除了用户可以键入(例如)yes或no的部分以外,或退出。我该怎么做? 这是我当前的代码:纸,岩石,剪刀游戏(c#)

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Console.WriteLine("Do you choose 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!"); 

        } 

       } 

      Console.ReadLine(); 
     } 
    } 
} 

帮助...

回答

1

删除最后ReadLine,使整个逻辑在while块如下:

bool keepPlaying = true; 
while (keepPlaying) { 
    //game logic here 

    Console.WriteLine("New game? y/n"); 
    ConsoleKeyInfo cki = Console.ReadKey(); //wait for player to press a key 
    keepPlaying = cki.KeyChar == 'y'; //continue only if y was pressed 
} 
+0

谢谢你,试试这种方法! :))) – Marko 2015-03-02 22:47:44

0

你可以用一切在do while的结构中:

class Program 
{ 
    static void Main(string[] args) 
    { 
     do 
     { 
      Console.WriteLine("Do you choose rock,paper or scissors"); 
      string userChoice = Console.ReadLine(); 
      // Rest of your code 
     }while(Console.ReadLine() == "yes"); 
    } 
} 

这将执行您的代码一次,然后再次每次用户输入yes

因此,您不需要任何私有变量,您就可以确保游戏至少执行一次。如果你想提示用户(单独一些“再次播放?”的行),你可以在}while部分之前的现有代码末尾执行此操作。

+0

谢谢,我会尝试它! :))) – Marko 2015-03-02 22:47:10

0

首先,当你调用r.Next(4);你的程序有机会产生从0到3所以可能性computerChoice为0,1,2,3 ...您的程序没有任何代码处理一个computerChoice一些当它等于0时,它会突然关闭。要解决这个第一个变化r.Next(4);r.Next(3);这将提供0,1,2的可能性。只需将else if (computerChoice == 3)更改为`else if(computerChoice == 0)'即可。这将修复你的程序。其次,正如其他答案所说,在整个程序中循环播放是明智的,因此您可以多次播放RPS。

-mnursey