2013-10-02 82 views
0

嘿所以我已經得到了我需要的一切,我的猜測遊戲和一切檢查和運行沒有問題,唯一的問題是我想要擁有一個勝算和一個猜測計數,以便用戶不能比計劃規定的得到更多的猜測。目前該計劃會告訴你你應該得到多少猜測,但由於單個if語句,它只會讓你猜一次,那麼我該如何去做這件事?我怎麼去創造一個勝利計數和猜測計數爲我的隨機數猜測遊戲C#

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

namespace GuessingGame 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      // Declare variables 
      Int32 currentGuess, guessCount, winCount, upperLimit, randomNumber; 
      double maxGuesses; 
      bool gameOver; 
      char playAgain; 
      Random random = new Random(); 



      // Display title 
      Console.WriteLine("Welcome to the high/low guessing game."); 
      //Request user input for upper limit 
      Console.WriteLine("Enter Upper range (e.g. 100):"); 
      upperLimit = Int32.Parse(Console.ReadLine()); 

      //Generate Random Number 
      randomNumber = random.Next(1, upperLimit); 
      maxGuesses = Math.Ceiling(Math.Log(upperLimit, 2) - 1); 

      // Begin game 
      Console.WriteLine("I picked a number between 1 and {0} you get {1} chances to guess it", upperLimit, maxGuesses); 

      // Begin Guessing Process 

      //Guess #1 
      { 
       Console.WriteLine(" Enter Guess #1: "); 
       currentGuess = Int32.Parse(Console.ReadLine()); 

       if (currentGuess == randomNumber) 
       { 
        Console.WriteLine("You got it!"); 
       } 
       if (currentGuess > randomNumber) 
       { 
        Console.WriteLine("Too High"); 
       } 
       if (randomNumber > currentGuess) 
       { 
        Console.WriteLine("Too Low"); 
       } 

        Console.ReadLine(); 
       } 
      } 
     } 
    } 

回答

1
 int gessNum = 0; 
     do 
     { 
      if (gessNum++ == maxGuesses){ 
       Console.WriteLine("You lost"); 
       break; 
      } 
      Console.WriteLine(string.Format(" Enter Guess {0}: ", gessNum)); 
      currentGuess = Int32.Parse(Console.ReadLine()); 

      if (currentGuess == randomNumber) 
      { 
       Console.WriteLine("You got it!"); 
      } 
      if (currentGuess > randomNumber) 
      { 
       Console.WriteLine("Too High"); 
      } 
      if (randomNumber > currentGuess) 
      { 
       Console.WriteLine("Too Low"); 
      } 
      Console.ReadLine(); 
     } while (currentGuess != randomNumber); 
+0

謝謝你,我一直在尋找更多的意見,但我可以分析這方面的工作。 – Shahze123