2015-11-07 76 views
-3

如果玩家得到的答案錯誤,我該如何重複此代碼/遊戲?在我的猜謎遊戲中創建循環

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) 
     { 
      Random RandomGenerator = new Random();// random number generator 
      Console.WriteLine("i have thought of a number between 1 and 100");//display message 
      // 
      // 
      int IN_RandomNum = RandomGenerator.Next(1, 100); //the range 
      //loop 10 times 
      for (int i = 0; i < 10; i++) //for loop created 
      { 
       int IN_Guess; //guessing integer 
       Console.Write("{0} turns left, enter your next guess>", 10 - i);//number of turns players has 
       // 
       IN_Guess = Convert.ToInt32(Console.ReadLine()); //string to number 
       //Now check if guess is same as generated 
       if (IN_Guess == IN_RandomNum)//if guess is equal to generated number 
       { 
        Console.WriteLine("correct in {0} turns", i + 1); //if guessed correctly 
        break; //breaking code 
       } 
       else if (IN_Guess > IN_RandomNum) //if guess is higher than generated number 
       { 
        Console.WriteLine("Too high");// if guessed number is too high 
       } 
       else //then... 
       { 
        Console.WriteLine("Too low"); // if guessed number is too low 
       } if (i==8) //on last turn display this message 
       { 
        Console.WriteLine("*YOU ONLY HAVE 1 GUESS LEFT!*"); //display this message 
       } 

      } 

      Console.WriteLine("please press enter to quit"); //display message 
      Console.ReadLine();//keeps application open until enter button hit 
     } 
    } 
} 

請幫助,因爲我實在找不出哪裏放while循環...香港專業教育學院跑出來的想法

+0

你能描述一下你的問題嗎?你已經有for循環了,應該就夠了 –

+0

把你的整個代碼放在另一個循環中,比如'while(1 )'或'while(someCondition)' –

回答

0

您需要在一個while循環來包裝的一切。

當它退出for循環時,如果它們贏或失,則返回頂端並重新開始,生成一個新的隨機數無關緊要。

之前閱讀下面的代碼,請參閱我的建議上面和自己嘗試吧,看看你是否能破解它

事情是這樣的:

static void Main(string[] args) 
{ 
    Random RandomGenerator = new Random();// random number generator 

    while (true) 
    { 
     Console.WriteLine("i have thought of a number between 1 and 100");//display message 
     bool guessedCorrect = false; 
     int IN_RandomNum = RandomGenerator.Next(1, 100); //the range 
                  //loop 10 times 
     for (int i = 0; i < 10; i++) //for loop created 
     { 
      int IN_Guess; //guessing integer 
      Console.Write("{0} turns left, enter your next guess>", 10 - i);//number of turns players has 
                      // 
      IN_Guess = Convert.ToInt32(Console.ReadLine()); //string to number 
                  //Now check if guess is same as generated 
      if (IN_Guess == IN_RandomNum)//if guess is equal to generated number 
      { 
       Console.WriteLine("correct in {0} turns", i + 1); //if guessed correctly 
       guessedCorrect = true; 
       break; //breaking code 
      } 
      else if (IN_Guess > IN_RandomNum) //if guess is higher than generated number 
      { 
       Console.WriteLine("Too high");// if guessed number is too high 
      } 
      else //then... 
      { 
       Console.WriteLine("Too low"); // if guessed number is too low 
      } 
      if (i == 8) //on last turn display this message 
      { 
       Console.WriteLine("*YOU ONLY HAVE 1 GUESS LEFT!*"); //display this message 
      } 
     } 

     if (guessedCorrect) 
     { 
      Console.WriteLine("Good job... Lets try again"); 
     } 
     else 
     { 
      Console.WriteLine("Better luck next timer... here we go"); 
     } 
    } 
} 

附:我每次嘗試都輸了,你的遊戲太難了:-(