我想創建一個遊戲,提示用戶輸入自己想要的速度和角度來確定一個隨機生成的目標區域結束。我遇到的問題是,我似乎無法到達的地方詢問玩家是否要再玩一次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;
}
}
初學者使用調試器..通過代碼步驟..並告訴我們,如果你在一個連續的循環或不..或你得到的錯誤是或否..?顯示所有相關的代碼..這是什麼'golf.Fire(userAngle,userVelocity)' – MethodMan
@MethodMan我沒有得到任何錯誤。我使用的調試和程序甚至從來沒有達到「Console.WriteLine(」你想再次發揮「?);行的方法火()是另一個類,我不認爲它真的有關聯,因爲錯誤是在試圖達到while循環的結尾,我的意思是,我可以發佈它,但我認爲這不會有幫助.. – corinne
那麼,在第一行設置一個斷點,並單步執行代碼,你會看到它被卡住的地方 –