C#循环回到前一部分代码

时间:2017-12-02 02:54:34

标签: c# loops

//Gender Creation:
while (correct == 0)
{
    do
    {
        Console.Clear();
        Console.WriteLine("Please choose a gender from the options below: ");
        Console.WriteLine("Male|Female|Random");
        Console.Write("Input: ");
        Gender = Console.ReadLine().ToUpper();
        if (Gender == "MALE")
        {
            Console.WriteLine("Is this the gender you wish your character to be? Enter Yes/No: ");
            Console.Write("Input: ");
            Input = Console.ReadLine().ToUpper();
            if (Input == "YES")
            {
                correct = 1;
            }
            else if (Input == "NO")
            {
                correct = 0;
            }
        }
        else if (Gender == "FEMALE")
        {
            Console.WriteLine("Is this the gender you wish your character to be? Enter Yes/No: ");
            Console.Write("Input: ");
            Input = Console.ReadLine().ToUpper();
            if (Input == "YES")
            {
                correct = 1;
            }
            else if (Input == "NO")
            {
                correct = 0;
            }
        }
        else if (Gender == "RANDOM")
        {
            correct = 2;
        }
        else
        {
            Console.WriteLine("ERROR, Please try again.");
            Gender = Console.ReadLine().ToUpper();
        }
    } while (correct == 0);

    //Random Gender Creation:
    if (correct == 2)
    {
        do
        {
            if (randgender == 1)
            {
                Console.WriteLine("The gender: MALE was randomly chosen");
                Console.WriteLine("Is this the gender you wish your character to be? Enter Yes/No: ");
                Console.Write("Input: ");
                Input = Console.ReadLine().ToUpper();
                if (Input == "YES")
                {
                    correct = 1;
                    Gender = "MALE";
                }
                else if (Input == "NO")
                {
                    correct = 2;
                }
            }
            else if (randgender == 2)
            {
                Console.WriteLine("The gender: FEMALE was randomly chosen");
                Console.WriteLine("Is this the race you wish your character to be? Enter Yes/No: ");
                Console.Write("Input: ");
                Input = Console.ReadLine().ToUpper();
                if (Input == "YES")
                {
                    correct = 1;
                    Gender = "FEMALE";
                }
                else if (Input == "NO")
                {
                    correct = 2;
                }
            }
        } while (correct == 2);
        correct = 0;
    }
    break;
}

correct = 2然后性别被随机生成时,如果用户在被问及是否对性别感到满意时输入否,则代码将一遍又一遍地循环随机性别生成器,不断地说随机性别每次随机数永远不会改变时是相同的,但是当correct = 0代码将在没有输入时进行时以及打印性别时它只是打印为RANDOM,因为这是用户最初选择的选项。

如何让它回到第一个while while循环来询问用户他们想要他们角色的性别?

2 个答案:

答案 0 :(得分:1)

正如Mayer建议的那样,您需要将应用分解成更小的部分。在编写C#之前,您可能还需要解决一些概念。

此示例可能会帮助您进一步了解,因为它说明了实现更好控制流的几种方法:

static void Main(string[] args)
{
    Console.Clear();

    var choosenGender = "";
    var wasChoiceConfirmed = false;

    while (wasChoiceConfirmed == false)
    {
        choosenGender = PromptForGender();

        switch (choosenGender)
        {
            case "RANDOM":
                var randomGender = GenerateRandomGender();
                wasChoiceConfirmed = PromptForGenderConfirmation(randomGender, true);
                break;
            case "MALE":
            case "FEMALE":
                wasChoiceConfirmed = PromptForGenderConfirmation(choosenGender);
                break;
            default:
                Console.WriteLine("Error, please try again. \n");
                break;
        }
    }
}

static string PromptForGender()
{
    Console.Write(
        "\nPlease choose a gender from the options below: \n" +
        "Male|Female|Random \n" +
        "Input:");
    return Console.ReadLine().Trim().ToUpper();
}

static bool PromptForGenderConfirmation(string gender, bool wasRandom = false)
{
    var randomVerbiage = wasRandom ? "randomly " : "";
    Console.Write(
        $"\nThe gender: {gender} was {randomVerbiage}chosen \n" +
        "Is this the gender you wish your character to be? Enter Yes/No: \n" +
        "Input: ");
    var confirmed = Console.ReadLine().Trim().ToUpper();
    return confirmed == "YES";
}

static string GenerateRandomGender()
{
    var randomNumber = new Random();
    return randomNumber.Next(0, 1) == 0 ? "FEMALE" : "MALE";
}

答案 1 :(得分:1)

我重构了你的代码,这有助于我更好地理解它。这为您提供了所需的结果,并且您不需要所有这些嵌套循环。

 public class Program
{
    public static void AskFirstQuestion()
    {
        Console.Clear();
        Console.WriteLine("Please choose a gender from the options below: ");
        Console.WriteLine("Male|Female|Random");
        Console.Write("Input: ");
        var gender = Console.ReadLine()?.ToUpper();
        if (gender == "MALE" || gender == "FEMALE")
        {
            HandleGenderSelection(gender);
        }
        else if (gender == "RANDOM")
        {
            HandleRandom();
        }
    }

    private static void HandleRandom()
    {
        var randomGender = GenerateRandomGender();
        Console.WriteLine($"The gender: {randomGender} was randomly chosen");
        Console.WriteLine("Is this the race you wish your character to be? Enter Yes/No: ");
        Console.Write("Input: ");
        var input = Console.ReadLine()?.ToUpper();
        switch (input)
        {
            case "YES":
                Console.WriteLine("OK");
                break;
            case "NO":
                AskFirstQuestion();
                break;
        }
    }

    private static string GenerateRandomGender()
    {
        //Have you logic to randomly create gender
        return "MALE";
    }

    private static void HandleGenderSelection(string gender)
    {
        Console.WriteLine("Is this the gender you wish your character to be? Enter Yes/No: ");
        Console.Write("Input: ");
        var input = Console.ReadLine()?.ToUpper();
        if (input == "YES")
        {
            Console.WriteLine("OK!");
        }
        else if (input == "NO")
        {
            AskFirstQuestion();
        }
    }


    static void Main(string[] args)
    {
        AskFirstQuestion();
    }
}