2015-10-14 55 views
0

鑄造詮釋我想明白,我怎麼能有這麼C#從字符串

string rock = 1 for inputting 
string paper = 2 for input 
string scissors = 3 for input 

所以我能夠使用int 1,2和3打印字符串輸出。

基本上控制檯應用程序具有用於輸入3個選項:要麼1(巖)2(紙)3(剪刀)和控制檯需要從生成的隨機數或者1,2-或3並確定您的輸入是否隨機跳動計算機。

string rock = (int)1; 
     string paper; 
     string scissors; 
     int input;   
     Random random = new Random(); 
     int RandomNumber = random.Next(1,4); 
     bool i = true; 

     Console.WriteLine("-- Weapons Menu --"); 
     Console.WriteLine("-------------------"); 
     Console.WriteLine("1] Rock"); 
     Console.WriteLine("2] Paper"); 
     Console.WriteLine("3] Scissors"); 
     Console.WriteLine("Choose Your Weapon [1, 2 or 3]:"); 

     input = int.Parse(Console.ReadLine()); 

     if (input == RandomNumber) 
      Console.WriteLine("You Tied The Computer This Round"); 
      Console.WriteLine("Player Chose ") 

對此的任何幫助將是偉大的,謝謝。我嘗試投出int,但我不能。

+0

也許枚舉輕鬆解決你的問題? – Dilshod

回答

4

使用Dictionary<int, string>

var choices = new Dictionary<int, string> { {1,"Rock"}, {2,"Paper"}, {3,"Scissors"} }; 

然後,只需訪問正確的元素:

Console.WriteLine("Player Chose {0}", choices[input]); 

你會想要一些驗證周圍到mak確保你不會嘗試訪問不存在的元素,或者用戶沒有輸入非數字值。

+0

我喜歡這種方法,虐待它一去。 –

+0

雖然我怎麼會也將電腦隨機生成顯示爲一個字符串?像玩家選擇{0},然後計算機選擇{1},選擇,隨機數? –

+0

想了很多,謝謝你 –

0
  1. 使用Int32.TryParse(Console.ReadLine(), out input);鑄件字符串爲整數。
  2. 對於鑄造詮釋爲字符串,你必須使用input.ToString();
  3. 然後,你可以使用Dictionary<int, string>達到目標。

而在你的代碼的問題是在這條線:string rock = (int)1; 而不是爲這個,你應該給string rock = "1";

+0

'Console.ReaLine'返回一個字符串,所以調用'ToString'不是必需的。 – Bauss

+0

絕對,謝謝我更新了代碼 –

0
 var random = new Random(); 
     while (true) 
     { 
      int RandomNumber = random.Next(1, 4); 
      var userinput = Console.ReadLine(); 
      if (Enumerable.Range(1, 3).Contains(Convert.ToInt16(userinput))) 
       if (userinput == RandomNumber.ToString()) 
        Console.WriteLine("Tied"); 
       else 
        Console.WriteLine("Check and see who won!"); 
      else 
       Console.WriteLine("Try Again!"); 
     } 
-1
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace RockPaperScissors 

{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 

      var choices = new Dictionary<int, string> { { 1, "Rock" }, { 2, "Paper" }, { 3, "Scissors" } }; 
      int input; 
      var randomly = new Dictionary<int, string> { { 1, "Rock" }, { 2, "Paper" }, { 3, "Scissors" } }; 

      Random random = new Random(); 
      int RandomNumber = random.Next(1, 4); 
      int Ties; 
      int Wins; 
      int Losses; 



      do 
      { 
       //Counter 
       Wins = 0; 
       Losses = 0; 
       Ties = 0; 
       Console.WriteLine("-- Weapons Menu --"); 
       Console.WriteLine("-------------------"); 
       Console.WriteLine("1] Rock"); 
       Console.WriteLine("2] Paper"); 
       Console.WriteLine("3] Scissors"); 
       Console.WriteLine("Choose Your Weapon [1, 2 or 3]:"); 
       input = int.Parse(Console.ReadLine()); 
       if (input == RandomNumber) 
        Console.WriteLine("You Tied The Computer"); 
       Ties++; 
       //Winning 
       if (input == 1 && RandomNumber == 3) 

       Console.WriteLine("You Beat The Computer :)"); 
       Wins++; 

       if (input == 2 && RandomNumber == 1) 

       Console.WriteLine("You Beat The Computer :)"); 
       Wins++; 

       if (input == 3 && RandomNumber == 2) 

       Console.WriteLine("You Beat The Computer :)"); 
       Wins++; 


       //Lossing 
       if (input == 1 && RandomNumber == 2) 

        Console.WriteLine("Sorry You Lost To The Computer :("); 
       Losses++; 

       if (input == 2 && RandomNumber == 3) 

       Console.WriteLine("You Beat The Computer :)"); 
       Losses++; 

       if (input == 3 && RandomNumber == 1) 

       Console.WriteLine("You Beat The Computer :)"); 
       Losses++; 










       Console.WriteLine(string.Format("Player Chose {0} : Computer Chose {1}", choices[input], randomly[RandomNumber])); 
       Console.WriteLine(); 
       Console.WriteLine("<Press Any Key To Continue...>"); 
       Console.WriteLine(); 



       Console.WriteLine("Player Wins  Computer Wins"); 
       Console.WriteLine("----------  --------------"); 
       Console.WriteLine(" {0}    {1}", Wins, Losses); 




       Console.ReadKey(); 
       } 
       while (input != 3) ; 
       Console.WriteLine("Thanks for playing!"); 
      }enter code here 
    } 
    } 



     STILL CANNOT SHOW THE COUNTER WHY