2014-02-21 49 views
2

我是一個初學者編程,所以任何提示將不勝感激!C#程序猜硬幣

我試圖在用戶猜測一個「拋硬幣」是否將產生(0)或尾部(1)使用隨機數發生器和一個環路頭創建程序的'硬幣'。該程序必須輸出所玩遊戲總數中的%的正確猜測。

我已經嘗試了很多不同的方法,這是很不正確的!這是令人沮喪的。

在此先感謝您的任何提示! -C

這基本上是我來最接近:

for (numPlays = 0; ; numPlays++) 
     { 
      Console.Write("\nWrite H/h to guess Heads, T/t to guess Tails, or Q/q to quit => "); 
      userChoice = Convert.ToChar(Console.ReadLine()); 

      if (userChoice == 'H' || userChoice == 'h') 
      { 
       if (compChoice == 0) 
       { 
        Console.WriteLine("\nYOU WON"); 
        numWins++; 
       } 
       else 
        Console.WriteLine("\nYOU LOSE"); 

       if (userChoice == 'Q' || userChoice == 'q') 
        if (compChoice == 1) 
        { 
         Console.WriteLine("\nYOU WON"); 
         numWins++; 
        } 
        else 
         Console.WriteLine("\nYOU LOSE"); 

       if (userChoice == 'q' || userChoice == 'Q') 
       { 
        percentWin = (double)(numWins/numPlays); 
        Console.WriteLine("\nYou won {0} out of {1} game(s) or {2:P} of the games played.", numWins, numPlays, percentWin); 
       } 
      } 

     } 

     Console.ReadKey(); 


    } 
} 
+3

你能告訴我們你的代碼在做什麼,爲什麼它不是你想要的? –

+3

電腦在哪裏做選擇?另外,compChoice是0還是1實際上是什麼意思? (爲什麼compChoice不是布爾值?) – panoptical

+1

你有什麼具體問題嗎?順便說一句,總是把你的括號,你的if-else語句非常混亂,並且非常容易出錯。如果您沒有任何特定問題並且只需要提示,[代碼評論](http://codereview.stackexchange.com/)會更合適。 –

回答

-2

這應該是沒有任何困難,你只需要做出明確的算法

讓我提出一個

Char playOrExit= 'y' 
Int32 gameCount = 0; 
Int32 Wins = 0; 
While (playOrExit.ToUpper.Equals('Y')) 
{ 
Console.Write("Press Y to continue playing and N to exit"); 
playOrExit = Convert.ToChar(Console.ReadLine()); 
//Computer choice will be math.random from 0 and 1 
//now take user choice 
//compare and show if he won or lost 
} 
if(gameCount >0) 
{ 
Console.WriteLine("\nYou played {0} times, out of which {1} game(s) were won.", gameCount,Wins); 
} 
+0

@匿名下來投票人,請告訴我原因 – Rex

0

只需調用此方法並玩你的遊戲!

public static void SomeMethod() 
    { 
     Console.Write("\nWrite H/h to guess Heads, T/t to guess Tails, or Q/q to quit => "); 
     char userChoice; 
     int numWins = 0; 
     int numPlays = 0; 
     double percentWin = 0d; 
     while ((userChoice = Convert.ToChar(Console.ReadLine())).ToString().ToLower() != "q") 
     { 
      numPlays++; 

      int compChoice = new Random().Next(2); 

      if (userChoice == 'H' || userChoice == 'h') 
      { 
       if (compChoice == 0) 
       { 
        Console.WriteLine("\nYOU WON"); 
        numWins++; 
       } 
       else 
       { 
        Console.WriteLine("\nYOU LOSE"); 
       } 
       continue; 
      } 
      if (userChoice == 'T' || userChoice == 't') 
      { 
       if (compChoice == 1) 
       { 
        Console.WriteLine("\nYOU WON"); 
        numWins++; 
       } 
       else 
       { 
        Console.WriteLine("\nYOU LOSE"); 
       } 
       continue; 
      }       
     } 

     if (numPlays != 0) 
     { 
      percentWin = (double)numWins/numPlays; 
     } 
     Console.WriteLine("\nYou won {0} out of {1} game(s) or {2:P} of the games played.", numWins, numPlays, percentWin); 
     Console.ReadKey(); 
    } 
+0

如果用戶鍵入多個字符,您的解決方案將炸燬。 –

+0

是的,我知道。但我試圖根據@ user3335589的要求寫入。它絕對不是一個通用的解決方案 –

1

我優化你的代碼一點點,我希望這有助於:

int numPlays = 0, numWins = 0; 
int compChoice = 0; 
char userChoice; 
double percentWin; 
Random rnd = new Random(); 
while (true) 
{ 
    Console.Write("\nWrite H/h to guess Heads, T/t to guess Tails, or Q/q to quit => "); 
    userChoice = Console.ReadKey().KeyChar; 
    compChoice = rnd.Next(0, 2); 
    if (char.ToLowerInvariant(userChoice) != 'q') 
    { 
      if (char.ToLowerInvariant(userChoice) == 'h' && compChoice == 0) 
      { 
       Console.WriteLine("\nYOU WON"); 
       numWins++; 
      } 
      else if (char.ToLowerInvariant(userChoice) == 't' && compChoice == 1) 
      { 
       Console.WriteLine("\nYOU WON"); 
       numWins++; 
      } 
      else 
      { 
       Console.WriteLine("\nYOU LOSE"); 
      } 
      numPlays++; 
    } 
    else 
    { 
      percentWin = (double) numWins/numPlays; 
      Console.WriteLine("\nYou won {0} out of {1} game(s) or {2:P} of the games played.", numWins, numPlays, percentWin); 
      break; 
    } 

} 

我覺得你並不需要一個解釋,所有的代碼是自explanatory.But你有一個明顯的錯誤,當你計算百分比:

percentWin = (double)(numWins/numPlays); 

在這裏你執行的是integer劃分,那麼結果鑄造double,這是點減去,因爲你已經失去了你的小數點。相反,你應該將其中一個操作數轉換爲double,這樣它將執行雙重除法,你將得到正確的結果。