Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当我不';我不希望它出现在我的';游戏选择';程序_C# - Fatal编程技术网

C# 当我不';我不希望它出现在我的';游戏选择';程序

C# 当我不';我不希望它出现在我的';游戏选择';程序,c#,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;

所以我再次陷入困境,我创建了一个“游戏选择”程序,允许用户选择他们的“技能”水平。我已经写了所有的代码,但是第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);



    }
}
类程序
{
静态void Main(字符串[]参数)
{
字符串名;
int一=1,二=2,三=3,四=4;
int答案;
int tripalarm=0;
字符串验证;
你叫什么名字;
name=Convert.ToString(Console.ReadLine());
Console.WriteLine(name+),游戏中有4个技能等级:”;
Console.WriteLine(“1.高级”+Environment.NewLine+“2.经验丰富”+Environment.NewLine+“3.一般”
+Environment.NewLine+“4.新手”);
做
{
答案=0;
Console.WriteLine(“您选择哪种技能级别?”);
answer=Convert.ToInt32(Console.ReadLine());
如果(答案>=5)
{
WriteLine(“对不起”+name+”您应该在1和4之间选择:”;
}

else if(answer这将稍微简化您的代码,并应纠正错误:

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

注意,它仍然不会检查用户是否输入了级别的整数(如果他们不输入,则会导致错误),并且任何其他“y”都被视为“n”.

我尝试了几乎所有的事情
都在使用调试器—您尝试过什么?是的,它没有检测到任何错误。我建议使用Switch语句而不是else ifsYeah,这不是调试器所做的—它允许您在代码执行时观察代码,甚至与代码交互,以便您可以删除错误(因此得名为debug)好的,现在我来试试,谢谢。非常感谢,我现在了解了开关函数的工作原理。