我必须创建一个程序,通过检查输入是数字,整数还是范围内来验证用户输入。如果不正确,则必须为每个提供错误消息。我无法弄清楚如何验证数字然后作为整数并为每个单独的问题提供错误消息(如果用户输入“Ten”,则提供消息“必须是数字”,如果用户输入“10.1”提供消息“必须是一个整数“。任何建议?
const int INPUT_MIN = 0;
const int INPUT_MAX = 50;
const int DAILY_COUNT = 7;
double totalVehicles = 0.0;
int avgDailyVehicles = 0;
int highestDay = 0;
int lowestDay = 0;
int dailySold = 0;
string userInput = "";
int[] salesArray = new int[DAILY_COUNT];
do {
for (int dayCount = 1; dayCount <= salesArray.Length; dayCount++)
{
Console.Write("Please enter the number of vehicles sold on day " + dayCount + ": ");
{
if (Int32.TryParse(Console.ReadLine(), out dailySold))
{
if ((dailySold >= INPUT_MIN && dailySold <= INPUT_MAX))
{
salesArray[dayCount - 1] = dailySold;
}
else
{
Console.WriteLine("\n" + dailySold + " is not a valid entry. ");
Console.WriteLine("The amount must be between {0} and {1}. Please try again.", INPUT_MIN, INPUT_MAX);
dayCount--;
}
}
else
{
Console.WriteLine("\n" + dailySold + " is not a valid entry. ");
Console.WriteLine("The amount must be a whole number. Please try again.");
dayCount--;
}
}
}
答案 0 :(得分:0)
对数字使用Decimal.TryParse,对整数使用Int64.TryParse。