2015-10-10 146 views
0

我想創建一個遊戲,提示用戶輸入自己想要的速度和角度來確定一個隨機生成的目標區域結束。我遇到的問題是,我似乎無法到達的地方詢問玩家是否要再玩一次while語句的結束。我認爲這是因爲「繼續」的路線。在「guess ++;」之後。是否有解決方法讓用戶繼續猜測,直到他們得到正確的答案並允許他們再次選擇播放?有麻煩達到while語句

 int guess = 1; 
     while (guess < 6) 
     { 
      Console.Write("Enter the initial velocity of the cannonball: "); 
      double userVelocity = double.Parse(Console.ReadLine()); 
      Console.Write("Enter the angle of the cannon (between 0 and 90 degrees): "); 
      double userAngle = double.Parse(Console.ReadLine()); 

      double targetDistance = distance - golf.Fire(userAngle, userVelocity); 

      if (guess <= 5 && targetDistance >= 0.5 || targetDistance <= -0.5) 
      { 
       Console.WriteLine("Miss! Your shot hit " + golf.Fire(userAngle, userVelocity) + " meters. The target is " + targetDistance + " \n meters away."); 
       Console.WriteLine(); 
       guess++; 
       continue; 
      } 

      if (targetDistance <= 0.5 && targetDistance >= -0.5) 
      { 
       Console.WriteLine(); 
       Console.WriteLine("Hit! Congratulations! You hit the target!"); 
       //continue; 
      } 

      Console.WriteLine("Would you like to play again? (Y/N)"); 
      String secondAnswer = Console.ReadLine(); 
      secondAnswer = secondAnswer.Trim(); 
      String againResponse = secondAnswer.ToLower(); 

      if (againResponse == "y") 
      { 
       continue; 
      } 
      else if (againResponse == "n") 
      { 
       break; 
      } 
     } 
+1

初學者使用調試器..通過代碼步驟..並告訴我們,如果你在一個連續的循環或不..或你得到的錯誤是或否..?顯示所有相關的代碼..這是什麼'golf.Fire(userAngle,userVelocity)' – MethodMan

+0

@MethodMan我沒有得到任何錯誤。我使用的調試和程序甚至從來沒有達到「Console.WriteLine(」你想再次發揮「?);行的方法火()是另一個類,我不認爲它真的有關聯,因爲錯誤是在試圖達到while循環的結尾,我的意思是,我可以發佈它,但我認爲這不會有幫助.. – corinne

+0

那麼,在第一行設置一個斷點,並單步執行代碼,你會看到它被卡住的地方 –

回答

1

變化

while (guess < 6) 

爲了

while (guess <=6) 

當變化是5,change++增量它至6 但是,外循環條件爲真,只有當變化是小於6 。將其更改爲較小或相等將再次進入循環並進入Y/N部分

另外要注意,你可能想改變這一行

if (guess <= 5 && targetDistance >= 0.5 || targetDistance <= -0.5) 

if (guess <= 5 && (targetDistance >= 0.5 || targetDistance <= -0.5)) 

否則,你的條件將最終成爲真正的隨時

targetDistance <= -0.5 
+0

謝謝,這個修正了它。 – corinne

+0

啊,我想過放圓括號,但是didn不認爲它會使差異;我猜是的!再次感謝。 – corinne

+0

您的版本評估類似於if((guess <= 5 && targetDistance> = 0.5)|| targetDistance <= -0.5)'。閱讀[評估的先後順序](https://msdn.microsoft.com/en-us/library/2bxt6kc4.aspx)獲取更多信息 – DanielS

1

我建議使用For循環代替寫入While語句用於循環預定次數的任何場景;這可以保護您避免遇到循環停止條件時的常見錯誤。

這是一個(未經測試的)替代版本。這並不完美,但我做了一些調整,以幫助使代碼更具可讀性,且不易出錯。我添加註釋來解釋一些變化,但請你如果有什麼是沒有意義的。

using System; 

namespace StackOverflow 
{ 
    class Program 
    { 

     Random randomNumberGenerator = new Random(DateTime.Now.Millisecond); 

     static void Main(string[] args) 
     { 
      int maxGuesses = 5; //putting this in a variable allows you to amend the difficulty 
      new Program(maxGuesses); 
      Console.WriteLine("Done; press enter to end"); 
      Console.ReadLine(); 
     } 

     //Kicks off the game 
     //On completion asks if the user wants to play again 
     //If so relaunches the game; if not exits. 
     Program(int maxGuesses) 
     { 
      bool playAgain = true; 
      while (playAgain) 
      { 
       Play(maxGuesses); 
       playAgain = PromptToPlayAgain(); 
      } 
     } 

     //returns: 
     //- true if user enters Y 
     //- false if user enters N 
     //if user enters anything else, keeps asking 
     bool PromptToPlayAgain() 
     { 
      String againResponse = ""; 
      while (againResponse != "y" && againResponse != "n") 
      { 
       Console.WriteLine("Would you like to play again? (Y/N)"); 
       againResponse = Console.ReadLine().Trim().ToLower(); 
      } 
      return againResponse == "y"; 
     } 

     double GetVelocity() 
     { 
      Console.Write("Enter the initial velocity of the cannonball: "); 
      return double.Parse(Console.ReadLine()); 
     } 
     double GetAngle() 
     { 
      Console.Write("Enter the angle of the cannon (between 0 and 90 degrees): "); 
      return double.Parse(Console.ReadLine()); 
     } 

     //generate a random distance 
     //returns a double where 100 <= d < 300 
     double GetRangeDistance() 
     { 
      return randomNumberGenerator.Next(1000, 3000)/10; //returns 
     } 

     //return true if the person's within .5 meters of the target; else false 
     bool CheckWinCondition(double targetDistance) 
     { 
      return targetDistance <= 0.5 && targetDistance >= -0.5; 
     } 
     //display message if successful 
     void ReportHit() 
     { 
      Console.WriteLine(); 
      Console.WriteLine("Hit! Congratulations! You hit the target!"); 
     } 
     //display message if missed 
     void ReportMiss(double shotDistance, double targetDistance) 
     { 
      Console.WriteLine("Miss! Your shot hit {0} meters. The target is {1} meters away.", shotDistance, targetDistance); //use {n} string formatting to put your numbers into your string 
      Console.WriteLine(); //NB: the blank line's the other way round to how you have it in ReportHit 
     } 
     //the game 
     void Play(int maxGuesses) 
     { 
      Golf golf = new Golf(); 
      double distance = GetRangeDistance(); 
      for (int guess = 1; guess <= maxGuesses; guess++) //use a for loop instead of while as we want to iterate a given number of times 
      { 
       double userVelocity = GetVelocity(); 
       double userAngle = GetAngle(); 
       //since we're using different variables for targetDistance and distance I assume 
       //that each shot's being taken from the tee; i.e. the player doesn't move to 
       //where the ball lands. 
       //Also I assume all shots go in a straight line between the tee and the hole 
       //and that angle is just up/down; not left/right, as otherwise the calc for 
       //target distance is off. 
       double shotDistance = golf.Fire(userAngle, userVelocity); //store this in a variable so we can reuse the result without recalculating 
       double targetDistance = distance - shotDistance; 
       if (CheckWinCondition(targetDistance)) 
       { 
        ReportHit(); 
        break; //exits the for loop early 
       } 
       else 
       { 
        ReportMiss(targetDistance, shotDistance); 
       } 
      } 
     } 

    } 
} 
+1

非常好的重構 – DanielS