我必須創建一個程序,通過檢查輸入是數字,整數還是範圍內來驗證用戶輸入。如果不正確,每個錯誤消息都必須提供。我不知道如何驗證數字,然後作爲整數,並提供每個單獨問題的錯誤消息(如果用戶輸入「十」提供消息「必須數字」,如果用戶輸入「10.1」提供消息「必須是一個整數」。任何建議?驗證用戶輸入爲數字,然後作爲整數使用Tryparse
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--;
}
}
}
你可以指出,正是在那裏你卡住了? – nozzleman
我不知道將Double.Tryparse放在哪裏,如果它是數字的,以及如果它不是數字條目,則會出現該消息。 –
10.0什麼應該給? –