防止 C# 因用户输入无效而崩溃
Preventing C# crashing with invalid user input
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();
}
}
}
如果这很混乱,我深表歉意,就像我说的那样,我是新手。谢谢大家的帮助!
要在值不是 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
}
不需要创建第二个变量attributes2。只需要使用第一个并不断递减它。这将确保您可以跟踪总共 25 个属性。
将您的例程包装在一个方法中,如果用户想像这样重新开始,则再次调用它:
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# 编码的新手,学习这门语言大约 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();
}
}
}
如果这很混乱,我深表歉意,就像我说的那样,我是新手。谢谢大家的帮助!
要在值不是 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 }
不需要创建第二个变量attributes2。只需要使用第一个并不断递减它。这将确保您可以跟踪总共 25 个属性。
将您的例程包装在一个方法中,如果用户想像这样重新开始,则再次调用它:
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 }