使用无效的用户输入防止C#崩溃

时间:2016-02-06 02:59:04

标签: c# crash

编码C#的全新,到学习语言约2周。我已经想到了一个移动游戏在我脑海里喋喋不休超过一年了,我的一个决议是学习今年的代码。我正在通过C#书的介绍工作,并掌握了基础知识,我正在编写一个旧的学校文本冒险作为技能强化者。我想这是一个3部分问题1)我试图编写一个系统,用户为属性类型选择1-10之间的值,但无法弄清楚如何阻止程序崩溃用户输入int以外的东西。 2)我尝试编写一种方式,游戏将从原始属性值中减去已分配的属性值,以确保用户最终不会超过25个.3)我想要包括底部的代码,如果用户不喜欢设置,则允许用户重新开始。以下是我的代码:

        Console.WriteLine("Now you must choose your basic attributes!. You have a total of 25 points to distribute over 5 core skills:");
        Console.WriteLine("Strength, Speed, Intelligence, Personality, and Ingenuity.");
        Console.WriteLine("Choose carefully! Values too high or too low may impact your ability to escape from the world!");
        int attributes = 25;
        int strength = -1;
        int speed = -1;
        int intelligence = -1;
        int personality = -1;
        int ingenuity = -1;
        Console.WriteLine("Attributes remaining: " + (attributes));
        do
        {

            Console.Write("Please choose a value for Strength (0-10): ");
            string strengthAsText = Console.ReadLine();
            strength = Convert.ToInt32(strengthAsText);
            if(strength > 10)
            {
                Console.WriteLine("You can't be that fast!");
            }
            else
            {
                Console.WriteLine("Good choice, please choose the next attribute.");
                attributes = 25 - strength;
                Console.WriteLine("Attributes remaining: " + (attributes));
            }
       }
        while (strength < 0 || strength > 10 && strength <= attributes);

        int attributes2 = (attributes - strength);
        do
        {

            Console.Write("Please choose a value for Speed (0-10): ");
            string speedAsText = Console.ReadLine();
            speed = Convert.ToInt32(speedAsText);
            if (speed > 10)
            {
                Console.WriteLine("You can't be that fast!");
            }
            else if (speed < attributes2)
            {
                Console.WriteLine("You do not have enough remaining attributes to be this fast. Please choose again.");
            }
            else
            {
                Console.WriteLine("Good choice, please choose the next attribute.");
                attributes2 = (attributes2 - speed);
                Console.WriteLine("Attributes remaining: " + (attributes2));
            }
        }
        while (speed < 0 || speed > 10 && speed <= attributes2);

        do
        {
            Console.Write("Please choose a value for Intelligence (0-10): ");
            string intelligenceAsText = Console.ReadLine();
            intelligence = Convert.ToInt32(intelligenceAsText);
            if (intelligence > 10)
            {
                Console.WriteLine("You can't be that smart!");
            }
            else
            {
                Console.WriteLine("Good choice, please choose the next attribute.");
                Console.WriteLine("Attributes remaining: " + (attributes - strength - speed - intelligence));
            }
        }
        while (intelligence < 0 || intelligence > 10 && intelligence <= attributes);

        do
        {
            Console.Write("Please choose a value for Personality (0-10): ");
            string personalityAsText = Console.ReadLine();
            personality = Convert.ToInt32(personalityAsText);
            if (personality > 10)
            {
                Console.WriteLine("You can't be that charismatic!");
            }
            else
            {
                Console.WriteLine("Good choice, please choose the next attribute.");
                Console.WriteLine("Attributes remaining: " + (attributes - strength - speed - intelligence - personality));
            }
        }
        while (personality < 0 || personality > 10 && personality <= attributes);

        do
        {
            Console.Write("Please choose a value for Ingenuity (0-10): ");
            string ingenuityAsText = Console.ReadLine();
            ingenuity = Convert.ToInt32(ingenuityAsText);
            if (ingenuity > 10)
            {
                Console.WriteLine("You can't be that creative!");
            }
            else
            {
                Console.WriteLine("Good choice, please choose the next attribute.");
                Console.WriteLine("Attributes remaining: " + (attributes - strength - speed - intelligence - personality - ingenuity));
            }
        }
        while (ingenuity < 0 || ingenuity > 10 && ingenuity <= attributes);

        Console.WriteLine("Congratulations, you have chosen wisely.");
        Console.WriteLine();
        Console.WriteLine("Your final attribute values are: ");
        Console.WriteLine();
        Console.WriteLine("Strength     " + (strength));
        Console.WriteLine("Speed        " + (speed));
        Console.WriteLine("Intelligence " + (intelligence));
        Console.WriteLine("Personaility " + (personality));
        Console.WriteLine("Ingenuity    " + (ingenuity));
        Console.WriteLine();
        Console.WriteLine("Please press any key to continue...");

        Console.ReadKey();
        Console.WriteLine("You begin you journey by awakening in a pile of what looks like starship debris.");
        Console.ReadKey();
    }
}

}

如果这很麻烦,我道歉,就像我说的那样,我对此感到陌生。谢谢大家的帮助!

1 个答案:

答案 0 :(得分:0)

  1. 要在值不是int时停止程序崩溃,请使用TryParse方法:

    bool result = Int32.TryParse(value,out number) //value is the input, number is the output
    If (result)
    {
        //value converted successfully 
    }
    else
    {
        //value failed to convert
    }
    
  2. 无需创建第二个变量attributes2。只需要使用第一个并继续递减它。这将确保您可以跟踪总共25个属性。

  3. 在一个方法中包装您的例程,如果用户想要重新开始,请再次调用它:

    string s = Console.ReadLine();
    If (s == "start over")
    {
        StartOver(); //this method contains your whole routine
    }
    else
    {
        //exit the application or whatever you want done here
    }