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();
}
}
}
如果这很麻烦,我道歉,就像我说的那样,我对此感到陌生。谢谢大家的帮助!
答案 0 :(得分:0)
要在值不是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
}