在到达while语句结束时遇到困难

本文关键字:遇到 结束 语句 while | 更新日期: 2023-09-27 18:17:01

我正在尝试创造一款游戏,促使用户输入他们想要的速度和角度,以确定随机生成的目标区域。我遇到的问题是,我似乎无法到达while语句的末尾,即询问玩家是否想要再次玩游戏。我认为这是因为"guess++;"之后的一行"continue;"。是否存在允许用户继续猜测直到他们得到正确答案并允许他们选择再次玩游戏的方法?

        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;
            }
        }

在到达while语句结束时遇到困难

change

while (guess < 6)

while (guess <=6)

当change为5时,change++将其加1至6但是,只有当变化小于6时,外部循环条件才为真。将其更改为less或equal将再次进入循环并到达Y/N部分

另一个注意,你可能想改变这行

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

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

否则,你的条件在任何时候都为真

targetDistance <= -0.5

我建议使用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);
                }
            }
        }
    }
}