C# 为什么我的计算结果不正确?

C# 为什么我的计算结果不正确?,c#,datetime,while-loop,C#,Datetime,While Loop,我正在制作一个计算预订日期的表格。 在星期五和星期六,一个房间的费用是150美元,而在其他日子的费用是120美元。我已经使用了一个while循环来设置它,但是出于某种原因,它一直在计算错误的价格 它应该是什么样子的: 它看起来像什么: 这是我的密码: int nights = 0; int price = 0; private void btnCalculate_Click(object sender, EventArgs e) { DateTime arrival

我正在制作一个计算预订日期的表格。 在星期五和星期六,一个房间的费用是150美元,而在其他日子的费用是120美元。我已经使用了一个while循环来设置它,但是出于某种原因,它一直在计算错误的价格

它应该是什么样子的:

它看起来像什么:

这是我的密码:

int nights = 0;
int price = 0;

private void btnCalculate_Click(object sender, EventArgs e)
    {
        DateTime arrivalDate = DateTime.Parse(txtArrivalDate.Text);
        DateTime departureDate = DateTime.Parse(txtDepartureDate.Text);
        TimeSpan ts = departureDate.Subtract(arrivalDate);
        nights = ts.Days;

        while (arrivalDate != departureDate)
        {
            DayOfWeek dow = arrivalDate.DayOfWeek;
            if (dow == DayOfWeek.Friday || 
                dow == DayOfWeek.Saturday)
            {
                price = 150;
            }
            else
            {
                price = 120;
            }

            arrivalDate = arrivalDate.AddDays(1);

        }

        txtNights.Text = nights.ToString();
        int totalPrice = price * nights;
        txtTotalPrice.Text = totalPrice.ToString();
        int average = totalPrice / nights;
        txtAvgPrice.Text = average.ToString();
        txtArrivalDate.Focus();
    }

你没有用
价格做任何事

它应该在while循环中使用

此外,您应该使用
.TotalDays
,而不是
.Days

比如:

            public static (decimal price, int nights) GetPrice
        (DateTime arrivalDate, DateTime departureDate)
    {
        //This code assumes there is no time component in the dates provided 

        if(departureDate < arrivalDate )
        {
            throw new ArgumentException("Arrival after Departure");
        }

        if (departureDate.Date == arrivalDate.Date)
        {
            //TODO
            return (0, 0);
        }

        Decimal totalPrice = 0;
        DateTime day = arrivalDate;
        while (day != departureDate)
        {
            totalPrice += GetRate(day.DayOfWeek);
            day = day.AddDays(1);
        }
        return (totalPrice, (int)(departureDate - arrivalDate).TotalDays);
    }

    private static decimal GetRate(DayOfWeek dow)
    {
        return (dow == DayOfWeek.Friday || dow == DayOfWeek.Saturday)
            ? 150
            : 120;
    }
public static(十进制价格,整数夜)GetPrice
(DateTime到达日期、DateTime离开日期)
{
//此代码假定提供的日期中没有时间组件
if(出发日期<到达日期)
{
抛出新的异常(“出发后到达”);
}
if(departureDate.Date==arrivalDate.Date)
{
//待办事项
返回(0,0);
}
十进制总价=0;
DateTime day=到达日期;
while(天!=出发日期)
{
totalPrice+=GetRate(day.DayOfWeek);
天=天。添加天(1);
}
返回(总价格(整数)(出发日期-到达日期)。总天数);
}
私有静态十进制GetRate(DayOfWeek dow)
{
回报率(道琼斯指数=星期五星期五星期六)
? 150
: 120;
}

您没有用
价格做任何事情

它应该在while循环中使用

此外,您应该使用
.TotalDays
,而不是
.Days

比如:

            public static (decimal price, int nights) GetPrice
        (DateTime arrivalDate, DateTime departureDate)
    {
        //This code assumes there is no time component in the dates provided 

        if(departureDate < arrivalDate )
        {
            throw new ArgumentException("Arrival after Departure");
        }

        if (departureDate.Date == arrivalDate.Date)
        {
            //TODO
            return (0, 0);
        }

        Decimal totalPrice = 0;
        DateTime day = arrivalDate;
        while (day != departureDate)
        {
            totalPrice += GetRate(day.DayOfWeek);
            day = day.AddDays(1);
        }
        return (totalPrice, (int)(departureDate - arrivalDate).TotalDays);
    }

    private static decimal GetRate(DayOfWeek dow)
    {
        return (dow == DayOfWeek.Friday || dow == DayOfWeek.Saturday)
            ? 150
            : 120;
    }
public static(十进制价格,整数夜)GetPrice
(DateTime到达日期、DateTime离开日期)
{
//此代码假定提供的日期中没有时间组件
if(出发日期<到达日期)
{
抛出新的异常(“出发后到达”);
}
if(departureDate.Date==arrivalDate.Date)
{
//待办事项
返回(0,0);
}
十进制总价=0;
DateTime day=到达日期;
while(天!=出发日期)
{
totalPrice+=GetRate(day.DayOfWeek);
天=天。添加天(1);
}
返回(总价格(整数)(出发日期-到达日期)。总天数);
}
私有静态十进制GetRate(DayOfWeek dow)
{
回报率(道琼斯指数=星期五星期五星期六)
? 150
: 120;
}

简而言之,
inttotalprice=price*nights
应删除此行,并且在while循环中,每种情况下,
price+=120
price+=150
totalPrice
可以简单地替换为
price

您没有使用while循环中设置的
price
<代码>价格
设置为120或150,但随后被下一个值覆盖(完全忽略上一个值)。因此,一旦您的代码退出while循环,就会使用最新的
price
集并乘以总夜数


因此,您的代码所做的是取最后一天(本例中为2016年2月1日)的
价格
,然后乘以总夜数。它应该做的是在循环中保持一个运行总数
price

简而言之,
inttotalprice=price*nights
应删除此行,并且在while循环中,每种情况下,
price+=120
price+=150
totalPrice
可以简单地替换为
price

您没有使用while循环中设置的
price
<代码>价格
设置为120或150,但随后被下一个值覆盖(完全忽略上一个值)。因此,一旦您的代码退出while循环,就会使用最新的
price
集并乘以总夜数


因此,您的代码所做的是取最后一天(本例中为2016年2月1日)的
价格
,然后乘以总夜数。它应该做的是在循环中保持一个运行总数
price

您只是将
price
设置为一个值,然后乘以天数。相反,您应该通过执行
totalPrice+=150来计算每天的总价格
总价+=120在循环中。你需要每天增加价格!在您的情况下,价格不更新您只需将
price
设置为一个值,然后乘以天数。相反,您应该通过执行
totalPrice+=150来计算每天的总价格
总价+=120在循环中。你需要每天增加价格!在您的情况下,使用
不会更新价格。天
仍会产生错误的结果
.TotalDays
应使用
。Days
仍将产生错误的结果<代码>应使用.TotalDays