当我不希望它在我的“游戏选择”程序中时,循环是循环的

时间:2018-01-09 22:47:07

标签: c#

所以我再次陷入困境,我创建了一个“游戏选择”程序,允许用户选择他们的“技能”级别。我已经编写了所有代码,但第24行的do循环导致了问题。当我选择比4更高的技能水平或者在“这是你想要的(y / n)”中输入'n'时,它不会循环。这是代码:

class Program
{
    static void Main(string[] args)
    {
        string name;
        int one = 1, two = 2, three = 3, four = 4;
        int answer;
        int tripalarm = 0;
        string verification;



        Console.WriteLine("What is your name?");
        name = Convert.ToString(Console.ReadLine());

        Console.WriteLine(name + ", there are 4 skill levels in this game:");
        Console.WriteLine("1. Advanced" + Environment.NewLine + "2. Experienced" + Environment.NewLine + "3. Average" 
            + Environment.NewLine + "4. Novice");

        do
        {
            answer = 0;

            Console.WriteLine("Which skill level do you choose?");
            answer = Convert.ToInt32(Console.ReadLine());


            if (answer >= 5)
            {
                Console.WriteLine("Sorry " + name + " you should choose between 1 and 4:");
            }
            else if (answer <= 4)
            {
                if (answer == one)
                {
                    Console.WriteLine("Thank you " + name + ", you have choosen level one");
                    Console.WriteLine("Is this what you want? (y/n)");
                    verification = Convert.ToString(Console.ReadLine());
                    if (verification == "y")
                    {
                        Console.WriteLine("Good " + name + " you have chosen level one you can now start the game!");
                    }
                    else 
                    {
                        tripalarm++;
                    }
                }
                else if (answer == two)
                {
                    Console.WriteLine("Thank you " + name + ", you have choosen level two");
                    Console.WriteLine("Is this what you want? (y/n)");
                    verification = Convert.ToString(Console.ReadLine());
                    if (verification == "y")
                    {
                        Console.WriteLine("Good " + name + " you have chosen level two you can now start the game!");
                    }
                    else
                    {
                        tripalarm++;
                    }
                }
                else if (answer == three)
                {
                    Console.WriteLine("Thank you " + name + ", you have choosen level three");
                    Console.WriteLine("Is this what you want? (y/n)");
                    verification = Convert.ToString(Console.ReadLine());
                    if (verification == "y")
                    {
                        Console.WriteLine("Good " + name + " you have chosen level three you can now start the game!");
                    }
                    else
                    {
                        tripalarm++;
                    }
                }
                else if (answer == four)
                {
                    Console.WriteLine("Thank you " + name + ", you have choosen level four");
                    Console.WriteLine("Is this what you want? (y/n)");
                    verification = Convert.ToString(Console.ReadLine());
                    if (verification == "y")
                    {
                        Console.WriteLine("Good " + name + " you have chosen level four you can now start the game!");
                    }
                    else
                    {
                        tripalarm++;
                    }
                }

            }

        } while (tripalarm == 0);



    }
}

我已经尝试了几乎所有东西,更改底部的while值不会改变任何东西。当它不应该时它仍然循环。

非常感谢,谢谢。

1 个答案:

答案 0 :(得分:0)

这会简化您的代码并纠正错误:

static void Main(string[] args)
{
    var levelSelected = false;
    var answer = 0;

    Console.WriteLine("What is your name?");

    string name = Console.ReadLine();

    Console.WriteLine($"{name}, there are 4 skill levels in this game:");
    Console.WriteLine("1. Advanced");
    Console.WriteLine("2. Experienced");
    Console.WriteLine("3. Average");
    Console.WriteLine("4. Novice");

    while (!levelSelected)
    {
        Console.WriteLine("Which skill level do you choose?");
        answer = Convert.ToInt32(Console.ReadLine());

        switch (answer)
        {
            case 1:
            case 2:
            case 3:
            case 4:
                Console.WriteLine($"Thank you {name}, you have choosen level {answer}");
                Console.WriteLine("Is this what you want? (y/n)");
                levelSelected = Console.ReadLine() == "y";
                break;
            default:
                Console.WriteLine($"Sorry {name} you should choose between 1 and 4:");
                break;
        }
    }

    Console.WriteLine($"Good {name} you have chosen level {answer} you can now start the game!");         
    var x = Console.ReadLine();
}

请注意,它仍然不会检查用户输入该级别的整数(如果它们不会导致错误)以及其他任何&#34; y&#34;被视为&#34; n&#34;。