Если вы ожидаете строки даты в определенном формате, то вы можете использовать метод TryParseExact
объекта DateTime
для создания DateTime
из строки, а затем использовать этоDateTime
объект для сравнения с вашими жестко закодированными датами.
Мы также можем использовать аналогичный метод TryParse
, чтобы получить целое число от пользователя при вводе числа людей.Это позволяет нам сообщать им, если они вводят неправильное значение (например, "two"
вместо "2"
), и просить их повторить попытку.
Например:
static void Main()
{
DateTime peakStartDate = new DateTime(2018, 6, 15);
DateTime peakEndDate = new DateTime(2018, 8, 15);
Console.Write("Please Enter the number of people: ");
// Use TryParse to get an integer from the user. If TryParse fails,
// it means they entered an invalid value, so ask them to do it again.
// Otherwise, numPeople will hold the integer value they entered
int numPeople;
while (!int.TryParse(Console.ReadLine(), out numPeople))
{
Console.WriteLine("Error: input was not a whole number.\n");
Console.Write("Please Enter the number of people: ");
}
// Now we can set the base flight price for the number of people
double flightPrice = 238 * numPeople;
// Get the arrival date from the user
Console.Write("\nPlease enter the arrival date (dd-MM-yyyy): ");
DateTime firstDate;
// If TryParseExact fails, they entered an incorrect format, so we
// keep asking them. If it succeeds, then firstDate will hold the value.
while (!DateTime.TryParseExact(Console.ReadLine(), "dd-MM-yyyy",
CultureInfo.CurrentCulture, DateTimeStyles.None, out firstDate))
{
Console.WriteLine("Error: input was not in correct format\n");
Console.Write("Please enter the arrival date (dd-MM-yyyy): ");
}
// Same process for departure date
Console.Write("\nPlease enter the departure date (dd-MM-yyyy):");
DateTime secondDate;
while (!DateTime.TryParseExact(Console.ReadLine(), "dd-MM-yyyy",
CultureInfo.CurrentCulture, DateTimeStyles.None, out secondDate))
{
Console.WriteLine("Error: input was not in correct format");
Console.Write("\nPlease enter the departure date (dd-MM-yyyy): ");
}
// If they're travelling during the peak period, increase the price
if (firstDate >= peakStartDate && secondDate <= peakEndDate)
{
flightPrice *= 1.2;
}
Console.ReadLine();
GetKeyFromUser("\nDone! Press any key to exit...");
}
Если вы хотите быть более гибким и позволить пользователю вводить однозначные дни и / или месяцы, вы можете создать массив допустимых форматов, например, так:
string[] validDateTimeFormats = {"dd-MM-yyyy", "d-MM-yyyy", "dd-M-yyyy", "d-M-yyyy"};
И затем передать массив в TryParseExact
метод:
while (!DateTime.TryParseExact(Console.ReadLine(), validDateTimeFormats,
CultureInfo.CurrentCulture, DateTimeStyles.None, out firstDate))
{