首页 > 解决方案 > C# 转到循环的开头(继续还是中断?)

问题描述

该程序随机创建一个用户需要猜测的 1 到 50 之间的数字。当用户的猜测不正确时,我想重新启动 while 循环。我尝试过使用 break 并继续这样做,但要么我做错了,要么这不是最好的解决方案。此外,当用户输入不正确时,系统会询问他们是否要再次猜测。我还尝试使用 break 退出该循环或继续获得类似的结果。有什么建议么?

Console.WriteLine("I am thinking of a whole number between 1 and 50. Enter your guess.");
        string userGuess;
        Random rnd = new Random();
        int compNum = rnd.Next(1, 50);
        int guessToInt;
        int numOfTries = 0;
        string cont;
        bool correct = false;
        //TODO Delete this line when finished
        Console.WriteLine($"Answer " + compNum);

        //Trying to get new user input if guess is wrong
        while (correct == false)
        {
            //correct = false;
            numOfTries++;
            userGuess = Console.ReadLine();
            guessToInt = Int32.Parse(userGuess);

            if (guessToInt == compNum)
            {
                numOfTries++;
                correct = true;
                Console.WriteLine($"Your guess is correct! The number was " + compNum + ". Congratulations. It only took you " + numOfTries + " guess(es).");
            }

            else if (guessToInt < 1 || guessToInt > 50)
            {
                Console.WriteLine("Incorrect input. Enter a whole number between 1 and 50. Try again.");
                //numOfTries++;
                correct = false;
                continue;
            }

            else if (guessToInt != compNum)
            {
                //How do I get new user input?
                Console.WriteLine("Your guess is incorrect. Would you like to try again? Y or N?");
                //numOfTries++;
                correct = false;
                cont = Console.ReadLine();

                if (cont == "Y")
                {
                    continue;
                }
                else Console.WriteLine("Bye."); Environment.Exit(0);
            }
        }

标签: c#loopsbreakcontinue

解决方案


几个问题:

  1. 您不必每次都将布尔值设置为 false。它在开始时已设置为 false
  2. else Console.WriteLine("Bye."); Environment.Exit(0);
    由于您没有添加大括号(我强烈建议您永远不要这样做),
    因此只有第一个命令将在语句下运行。
    “Environment.Exit”总是会发生。

对于其余部分,我将修复添加到您的代码中。

Console.WriteLine("I am thinking of a whole number between 1 and 50. Enter your guess.");
string userGuess;
Random rnd = new Random();
int compNum = rnd.Next(1, 50);
int guessToInt;
int numOfTries = 0;
string cont;
bool correct = false;
//TODO Delete this line when finished
Console.WriteLine($"Answer " + compNum);

//Trying to get new user input if guess is wrong
while (correct == false)
{
    //correct = false;
    numOfTries++;
    userGuess = Console.ReadLine();
    guessToInt = Int32.Parse(userGuess);

    if (guessToInt == compNum)
    {
        numOfTries++;
        correct = true;
        Console.WriteLine($"Your guess is correct! The number was " + compNum + ". Congratulations. It only took you " + numOfTries + " guess(es).");
    }

    else if (guessToInt < 1 || guessToInt > 50)
    {
        Console.WriteLine("Incorrect input. Enter a whole number between 1 and 50. Try again.");
        //numOfTries++;
        //**correct = false; --> No need, correct was already false
        //**continue; --> No need, will continue anyway
    }

    else if (guessToInt != compNum)
    {
        //How do I get new user input?
        Console.WriteLine("Your guess is incorrect. Would you like to try again? Y or N?");
        cont = Console.ReadLine();
        //numOfTries++;
        //**correct = false; --> No need, correct was already false

        //** if (cont == "Y") --> No need, will continue anyway
        //** {
        //**     continue;
        //** }

        // else Console.WriteLine("Bye."); Environment.Exit(0); --> 
        //** "else" without brackets will 
        if (cont != "Y") 
        {
            break;
        }
        Console.WriteLine("Enter your next guess:");
    }
}

注意:我没有触及“numOfTries”功能,因为问题中没有提到


推荐阅读