2017-07-18 46 views
0

我做了一個小遊戲,但它不工作,因爲我想。 這是一個猜數字遊戲。玩家有10次機會。但是,我的問題。這會增加playerChance的價值,直到達到10.我可以用什麼來代替while?猜猜C#中的玩家機會數量

它的輸出是:

Guess the number game! Do you wanna play? y/n 
y 
The game started! What is your guess? 
78 
Your number was too high! 
Your number was too high! 
Your number was too high! 
Your number was too high! 
Your number was too high! 
Your number was too high! 
Your number was too high! 
Your number was too high! 
Your number was too high! 

代碼:

namespace GuessTheNumberGame_v1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Console.WriteLine("Guess the number game! Do you wanna play? y/n"); 
      if (Console.ReadKey(true).KeyChar == 'y') 
      { 
       Console.WriteLine("The game started! What is your guess?"); 
       int playerGuess = Convert.ToInt32(Console.ReadLine()); 

       Random r = new Random(); 
       int compGuess = r.Next(1, 101); 

       int playerScore = 0; 
       int playerChance = 0; 

       bool play = true; 

       do 
       { 
        while (playerChance < 10) 
        { 
         if (playerGuess > compGuess) 
         { 
          ++playerChance; 
          Console.WriteLine("Your number was too high!"); 
         } 
         else if (playerGuess < compGuess) 
         { 
          Console.WriteLine("Your number was too small!"); 
          ++playerChance; 
         } 
         else 
         { 
          ++playerScore; 
          Console.WriteLine("You win! \nYou have: " + playerScore + " points!"); 
          playerChance = 0; 
         } 
        } 

        Console.WriteLine("Do you wanna play again? y/n"); 
        if (Console.ReadKey(true).KeyChar == 'n') 
         play = false; 
       } while (play);  
      } 

      else 
      { 
       Console.ReadKey(); 
      } 
     } 
    } 
} 
+0

當你贏了設置'playerChance = 11;' – Hackerman

+1

也許你想迭代,而'playerChance> 0'?此外,您應該減少用戶猜測而不是增加時的機會數量。 –

+0

這看起來像一個偉大的遊戲,但也許這個問題更適合[堆棧溢出代碼評論](https://codereview.stackexchange.com/) – Yonlif

回答

4

裏面你有四個方面的問題。

第一個是你不跳出內部循環,當你完成:

    else 
        { 
         ++playerScore; 
         Console.WriteLine("You win! \nYou have: " + playerScore + " points!"); 
         playerChance = 0; 
         break; // <-- "breaks out" of the innermost loop early 
        } 

一個break無條件退出循環。與continue比較,其中重新啓動循環並重新檢查循環條件。

第二個是「輸入用戶猜測」邏輯需要裏面的內部循環!

第三個是如果用戶輸入的不是數字,你的代碼會崩潰。使用int.TryParse,如果用戶的輸入不是數字,則讓他們再試一次。你也需要一個循環!

第四是你沒有「你輸了」的信息。你能看到如何實現?

最後:如果你開始把你的程序分解成更小的方法,你的問題就會變得更容易解決。假設我們有一個方法:

// Asks the user to guess; returns true if the user guessed correctly. 
private static bool Guess(int answer) 
{ 
    int playerGuess = GetIntegerFromUser("What is your guess?"); 
    // TODO: write GetIntegerFromUser 
    if (playerGuess > answer) 
    { 
    Console.WriteLine("Your number was too high!"); 
    return false; 
    } 
    else if (playerGuess < answer) 
    { 
    Console.WriteLine("Your number was too small!"); 
    return false; 
    } 
    else 
    { 
    Console.WriteLine("You win!"); 
    return true; 
    } 
} 

現在你的「主」循環得到更容易閱讀

while (playerChance < 10) 
{ 
    if (Guess(compGuess)) 
    { 
    // TODO: Deal with winning 
    } 
    ... 

等等。 識別小問題,用一種方法解決每個問題,然後將這些解決方案放在一起

+0

使用休息也是我的建議。我還會將while循環更改爲while(true),並在playerChance == 10的循環內添加一個檢查,然後輸出相應的消息並休息一下。它保持檢查最大嘗試的邏輯,顯示消息並一起退出循環。外部循環已經設置了playerChance = 0,所以它不需要在內部循環中設置。 – Marc

0

變化

while (playerChance < 10) 

while (playerChance < 10 && playerGuess != compGuess) 

你必須輸入在每次迭代一個新的猜測,所以移動

int playerGuess = Convert.ToInt32(Console.ReadLine()); 

爲了這個循環

int playerGuess = -1; 
... 
while (playerChance < 10 && playerGuess != compGuess) { 
    playerGuess = Convert.ToInt32(Console.ReadLine()); 
1

要回答這個問題,它不能按預期工作的原因是因爲您只讀取一次數字輸入。每次有不正確的答案時,您都需要得到新的猜測。

有一些需要解決的一些其他項目:

  • 時,他們得到的第10 之前正確答案嘗試會發生什麼事?
  • 用戶失去時會看到什麼?
  • 他們在玩另一場比賽時是否會得到正確的提示?

其他建議我對你的一般發展:

  • 當讀取一個值,不要以爲這將是正確的類型
  • 考慮針對特定任務創建的小功能。這將幫助您在將來進行測試,更新,故障排除和維護。在這種情況下,檢查答案的函數可能會簡化一些邏輯。