使用无效用户输入防止C#崩溃
对C#编码是全新的,学习该语言约2周。一年多以来,我一直有一个关于手机游戏的想法,今年我的一个决心就是学会编码。我正在写一本C#book的介绍,在掌握了基本知识后,我开始尝试编写一本老派的基于文本的冒险书,作为技能强化器。我猜这是一个由三部分组成的问题1)我试图编写一个系统,其中用户为属性类型选择1-10之间的值,但不知道如果用户输入的不是int,如何阻止程序崩溃。2)我试图编写一种游戏将从原始属性值中减去已分配属性,以确保用户最终不能超过25个。3) 我想在底部包含一个代码,允许用户在不喜欢设置时重新开始。下面是我的代码:使用无效用户输入防止C#崩溃,c#,crash,C#,Crash,对C#编码是全新的,学习该语言约2周。一年多以来,我一直有一个关于手机游戏的想法,今年我的一个决心就是学会编码。我正在写一本C#book的介绍,在掌握了基本知识后,我开始尝试编写一本老派的基于文本的冒险书,作为技能强化器。我猜这是一个由三部分组成的问题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();
}
}
Console.WriteLine(“现在你必须选择你的基本属性了!”。你总共有25分可以分配给5项核心技能:”;
WriteLine(“力量、速度、智慧、个性和创造力”);
WriteLine(“小心选择!值太高或太低都可能影响你逃离世界的能力!”);
int属性=25;
整数强度=-1;
整数速度=-1;
智力智力=-1;
智力人格=-1;
智力智力=-1;
WriteLine(“剩余属性:”+(属性));
做
{
控制台。写入(“请选择强度值(0-10):”;
string StrengthHastext=Console.ReadLine();
强度=转换为32(强度文本);
如果(强度>10)
{
WriteLine(“你不能那么快!”);
}
其他的
{
WriteLine(“好的选择,请选择下一个属性。”);
属性=25-强度;
WriteLine(“剩余属性:”+(属性));
}
}
而(强度<0 | |强度>10和强度10)
{
WriteLine(“你不能那么快!”);
}
否则如果(速度<属性2)
{
WriteLine(“您没有足够的剩余属性以达到如此快的速度。请重新选择。”);
}
其他的
{
WriteLine(“好的选择,请选择下一个属性。”);
属性2=(属性2-速度);
WriteLine(“剩余属性:”+(属性2));
}
}
同时(速度<0 | |速度>10和速度10)
{
WriteLine(“你不可能那么聪明!”;
}
其他的
{
WriteLine(“好的选择,请选择下一个属性。”);
WriteLine(“属性剩余:”+(属性-强度-速度-智能));
}
}
而(智力<0 | |智力>10&&10)
{
WriteLine(“你不可能那么有魅力!”;
}
其他的
{
WriteLine(“好的选择,请选择下一个属性。”);
WriteLine(“属性剩余:”+(属性-强度-速度-智能-个性));
}
}
while(个性<0 | |个性>10&&10)
{
WriteLine(“你不可能那么有创意的!”);
}
其他的
{
WriteLine(“好的选择,请选择下一个属性。”);
WriteLine(“属性剩余:”+(属性-强度-速度-智能-个性-独创性));
}
}
而(独创性<0 | |独创性>10&&独创性
要在值不是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
}
谢谢你Vivek!第一次机会我必须正确使用一种方法,它工作得非常好。