gpt4 book ai didi

c# - 防止 C# 由于无效用户输入而崩溃

转载 作者:行者123 更新时间:2023-12-01 19:14:43 25 4
gpt4 key购买 nike

C# 编码新手,学习该语言约 2 周。制作一款手机游戏的想法在我脑海中盘旋了一年多,我的决心之一就是今年学习编码。我正在阅读一本 C# 简介书籍,并掌握了基础知识后,我正在尝试编写基于老式文本的冒险游戏作为技能强化工具。我想这是一个由 3 部分组成的问题 1) 我正在尝试编写一个系统,其中用户为属性类型选择 1-10 之间的值,但无法弄清楚如果用户输入其他内容如何阻止程序崩溃比一个整数。 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. 要在值不是 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
    }

关于c# - 防止 C# 由于无效用户输入而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35236839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com