我无法弄清楚我做错了什么。我想要做的是获取用户输入并验证它是一个数字,在1-4之间,没有其他任何事情崩溃该程序。但我不能让tryparse数字验证工作。它不会运行里面的代码。 1和5之外的任何整数都不会抛出错误,但输入小数,字符串等。这是一个明显的解决方案,但我似乎无法弄明白。
class Program
{
static void Main(string[] args)
{
string[] ColorsArray = new string[12] { "blue", "red", "green", "yellow", "blue", "green", "blue", "yellow", "red", "green", "red", "yellow" };
float[] LengthArray = new float[12] { 1.3f, 1.4f, 5.6f, 1.5f, 3.5f, 5.4f, 1.2f, 6.5f, 4.4f, 4.1f, 3.3f, 4.9f };
Console.WriteLine("Select a color of Fish by entering the corresponding number \r\n 1. Blue \r\n 2. Yellow \r\n 3. Red \r\n 4. Green");
string ColorChoiceNumber = Console.ReadLine();
int ColorChoiceNumberInt = int.Parse(ColorChoiceNumber);
if (ColorChoiceNumberInt == 1)
{
Console.WriteLine("The Biggest Blue Fish is Fish Number ");
}
else if (ColorChoiceNumberInt == 2)
{
Console.WriteLine("The Biggest Yellow Fish is Fish Number");
}
else if (ColorChoiceNumberInt == 3)
{
Console.WriteLine("The Biggest Red Fish is Fish Number");
}
else if (ColorChoiceNumberInt == 4)
{
Console.WriteLine("The Biggest Green Fish is Fish Number");
}
else if (!(int.TryParse(ColorChoiceNumber, out ColorChoiceNumberInt)) && ColorChoiceNumberInt < 1 && ColorChoiceNumberInt > 4)
{
Console.WriteLine("Please only enter a number. It must be 1-4.");
ColorChoiceNumber = Console.ReadLine();
int.TryParse(ColorChoiceNumber, out ColorChoiceNumberInt);
}
else
{
Console.WriteLine("Please only enter a number. It must be 1-4.");
ColorChoiceNumber = Console.ReadLine();
int.TryParse(ColorChoiceNumber, out ColorChoiceNumberInt);
}
}
}
}
答案 0 :(得分:0)
我开始回答,但发现你的代码存在很多问题,因此重构似乎更容易,让你通过阅读工作答案来学习。
试试这个:
string[] ColorsArray = new string[12] { "blue", "red", "green", "yellow", "blue", "green", "blue", "yellow", "red", "green", "red", "yellow" };
float[] LengthArray = new float[12] { 1.3f, 1.4f, 5.6f, 1.5f, 3.5f, 5.4f, 1.2f, 6.5f, 4.4f, 4.1f, 3.3f, 4.9f };
Console.WriteLine("Select a color of Fish by entering the corresponding number \r\n 1. Blue \r\n 2. Yellow \r\n 3. Red \r\n 4. Green");
bool asking = true;
while (asking)
{
string ColorChoiceNumber = Console.ReadLine();
int ColorChoiceNumberInt;
if (int.TryParse(ColorChoiceNumber, out ColorChoiceNumberInt))
{
if (ColorChoiceNumberInt >= 1 && ColorChoiceNumberInt <= 4)
{
asking = false;
string[] fish = new string[] { "Blue", "Yellow", "Red", "Green" };
Console.WriteLine("The Biggest {0} Fish is Fish Number ", fish[ColorChoiceNumberInt - 1]);
}
else
{
Console.WriteLine("Please only enter a number. It must be 1-4.");
}
}
else
{
Console.WriteLine("Please only enter a number. It must be 1-4.");
}
}
答案 1 :(得分:0)
通常,要获得用户的有效输入,您只需要做一些事情:
int
int.TryParse
变量
out
参数传递给int.TryParse
int.TryParse
的返回值以及变量的边界检查作为循环的一部分,因此用户被迫在指定范围内输入有效数字例如,以下代码首先显示选项,然后在用户输入之前不断询问用户输入有效信息:
// Present the options
Console.WriteLine(" 1. Blue \r\n 2. Yellow \r\n 3. Red \r\n 4. Green");
// This variable will hold their input after the TryParse call
int ColorChoiceNumberInt;
// Continue to ask for input until they enter an integer between 1 and 4
do
{
Console.Write("Select a color of Fish by entering the corresponding number (1-4): ");
} while (!int.TryParse(Console.ReadLine(), out ColorChoiceNumberInt) ||
ColorChoiceNumberInt < 1 ||
ColorChoiceNumberInt > 4);
我过去所做的就是为此创建一个帮助函数,因此逻辑可以在整个程序中轻松重复使用:
public static int GetIntFromUser(string prompt, int minValue = int.MinValue,
int maxValue = int.MaxValue)
{
int input;
do
{
Console.Write(prompt);
} while (!int.TryParse(Console.ReadLine(), out input) ||
input < minValue ||
input > maxValue);
return input;
}
使用这种辅助方法,您的主要代码突然变得更加简化:
Console.WriteLine(" 1. Blue \r\n 2. Yellow \r\n 3. Red \r\n 4. Green");
int input = GetIntFromUser("Select a fish color (1-4): ", 1, 4);
Console.WriteLine("You entered: " + input);
<强>输出强>