Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 需要从用户输入中获取2个日期,并在if语句中进行比较_C#_Date_Datetime_Int - Fatal编程技术网

C# 需要从用户输入中获取2个日期,并在if语句中进行比较

C# 需要从用户输入中获取2个日期,并在if语句中进行比较,c#,date,datetime,int,C#,Date,Datetime,Int,我要求用户输入两个日期,例如起飞和返回航班,然后我需要能够在if语句中进行比较,如果航班在夏季范围内,则价格要高出20% 感谢所有帮助,Visual Studio C# 这就是我所尝试的: String firstDate; String secondDate; double people; Console.WriteLine("Please Enter the number of people:")

我要求用户输入两个日期,例如起飞和返回航班,然后我需要能够在if语句中进行比较,如果航班在夏季范围内,则价格要高出20%

感谢所有帮助,Visual Studio C#

这就是我所尝试的:

String firstDate;
            String secondDate;
            double people;
            Console.WriteLine("Please Enter the number of people:")
            people = Convert.ToDouble(Console.ReadLine());
            double flightPrice = 238;
            Console.Write("Please enter the arrival date (dd-MM-yyyy):");
            firstDate = Console.ReadLine();
            Console.Write("Please enter the departure date (dd-MM-yyyy):");
            secondDate = Console.ReadLine();

            if (firstDate >= "15-06-2018" && secondDate <= "15-08-2018")
            {
                flightPrice = 238 * 1.20 * people;
            }
            else
            {
                flightPrice = 238 * people;
            }

            Console.ReadLine();
stringfirstdate;
字符串secondDate;
双重人格;
Console.WriteLine(“请输入人数:”)
people=Convert.ToDouble(Console.ReadLine());
双飞价格=238;
控制台。写入(“请输入到达日期(dd-MM-yyyy):”;
firstDate=Console.ReadLine();
控制台。填写(“请输入出发日期(dd-MM-yyyy):”;
secondDate=Console.ReadLine();

如果(firstDate>=“15-06-2018”&&secondDate=不能应用于“string”和“string”类型的操作数。

在比较日期时间对象之前,需要将日期字符串转换为日期时间对象,则不能直接将日期作为字符串进行比较

DateTime arrivalDate = DateTime.Parse(firstDate);
DateTime departureDate = DateTime.Parse(secondDate);

DateTime summerStart = DateTime.Parse("15-06-2018");
DateTime summerEnd = DateTime.Parse("15-08-2018");

if (arrivalDate >= summerStart && departureDate <= summerEnd)
{
    flightPrice = 238 * 1.20 * people;
}
else
{
        flightPrice = 238 * people;
}
DateTime-arrivalDate=DateTime.Parse(firstDate);
DateTime departureDate=DateTime.Parse(secondDate);
DateTime summerStart=DateTime.Parse(“15-06-2018”);
DateTime summerEnd=DateTime.Parse(“15-08-2018”);
如果(arrivalDate>=夏季开始和出发日期请尝试以下操作:

            string firstDate = "10-08-2018";
            DateTime _firstDate = DateTime.ParseExact(firstDate, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture);
            string secondDate = "10-08-2018";
            DateTime _secondDate = DateTime.ParseExact(secondDate, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture);

            if (_firstDate >= DateTime.ParseExact("15-06-2018", "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture) &&
                _secondDate <= DateTime.ParseExact("15-08-2018", "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture))
            {
            }
string firstDate=“10-08-2018”;
DateTime _firstDate=DateTime.ParseExact(firstDate,“dd-MM-yyyy”,System.Globalization.CultureInfo.InvariantCulture);
字符串secondDate=“10-08-2018”;
DateTime _secondDate=DateTime.ParseExact(secondDate,“dd-MM-yyyy”,System.Globalization.CultureInfo.InvariantCulture);
如果(_firstDate>=DateTime.ParseExact(“2018年6月15日”,“年月日”,System.Globalization.CultureInfo.InvariantCulture)&&

_secondDate如果希望日期字符串采用特定格式,则可以使用
DateTime
对象的方法从字符串中创建
DateTime
,然后使用该
DateTime
对象与硬编码日期进行比较

当用户输入人数时,我们也可以使用类似的方法从用户那里获取一个整数。这样,如果用户输入了错误的值(如
“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...");
}
然后将数组传递给
TryParseExact
方法:

while (!DateTime.TryParseExact(Console.ReadLine(), validDateTimeFormats,
    CultureInfo.CurrentCulture, DateTimeStyles.None, out firstDate))
{

你试过什么了吗?如果到目前为止你已经做了多少?试着发布一些代码来帮助我们了解你的问题所在。这是什么API?ASP.NET?WinForms?WPF?UWP?抱歉,我是新来的,这是学校的C#作业。将编辑帖子,放入我尝试过的代码。人数应该是整数。谢谢,但我收到一个错误r'字符串未被识别为有效的DateTime。对于DateTime=summerStart etcThanks,我需要这两个日期作为用户输入。首先,用户输入是这两个字符串。我只是硬编码了值以显示所需的所有代码。谢谢,它起作用了!最后一件事是,有没有办法打印这两个日期中的天数?String totalDays=(_secondDate-_firstDate).TotalDays.ToString()的;嘿,最后一件事,假设用户输入字符串或错误的日期,我如何进行while循环或其他操作以确保日期的格式正确?谢谢。尽管
ParseExact
会在用户输入无效输入时引发异常。
TryParseExact
允许您处理这种情况而不会发生爆炸…)
while (!DateTime.TryParseExact(Console.ReadLine(), validDateTimeFormats,
    CultureInfo.CurrentCulture, DateTimeStyles.None, out firstDate))
{