2015-03-02 112 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