简体   繁体   English

C# Go 到循环开始(继续还是中断?)

[英]C# Go to beginning of loop (continue or break?)

This program randomly creates a number between 1 and 50 that the user needs to guess.该程序随机创建一个用户需要猜测的 1 到 50 之间的数字。 I want to restart the while loop when the user's guess is incorrect.当用户的猜测不正确时,我想重新启动 while 循环。 I have tried using break and continue to do this, but either I am doing it wrong or it is not the best solution.我尝试过使用 break 并继续这样做,但要么我做错了,要么这不是最好的解决方案。 Also, when the user input is incorrect they are asked if they want to guess again.此外,当用户输入不正确时,系统会询问他们是否要再次猜测。 I also tried exiting that loop with break or continue with similar results.我还尝试使用 break 退出该循环或继续获得类似的结果。 Any suggestions?有什么建议么?

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

Few problems:几个问题:

  1. You don't have to set the boolean to false each time.您不必每次都将 boolean 设置为 false。 It is already set to false at start它在开始时已设置为 false
  2. else Console.WriteLine("Bye."); Environment.Exit(0);
    Since you didn't add curly brackets (which I strongly advise you to never do),由于您没有添加大括号(我强烈建议您不要这样做),
    Only the first command will operate under the statement.只有第一个命令会在语句下运行。
    The "Environment.Exit" will always occur. “Environment.Exit”总是会发生。

For the rest, I added the fixes to your code.对于 rest,我将修复程序添加到您的代码中。

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:");
    }
}

NOTE: I haven't touched the "numOfTries" feature since not mentioned in the question注意:我没有触及“numOfTries”功能,因为问题中没有提到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM