简体   繁体   English

C#为什么我的计算没有正确添加?

[英]C# Why is my calculations not adding correctly?

I am making a form that calculates reservation dates. 我正在制作一个计算预订日期的表格。 On Fridays and Saturdays the charge for a room is $150 while on the rest of the days the charge is $120. 在周五和周六,一间客房的费用为150美元,而在其他日子则为120美元。 I have used a while loop in order to set this up but for some reason it keeps calculating the wrong prices. 我使用了while循环来进行设置,但是由于某种原因,它一直在计算错误的价格。

What it should look like: 它应该是什么样的:

在此处输入图片说明

What it looks like: 看起来像什么:

在此处输入图片说明

Here is my code: 这是我的代码:

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();
    }

In short, int totalPrice = price * nights; 简而言之, int totalPrice = price * nights; this line should be removed, and in the while loop, price += 120 or price += 150 in each condition. 该行应删除,在while循环中,每种情况下的price += 120price += 150 totalPrice can simply be replaced by price . totalPrice可以简单地由price代替。

You're not using the price set in while loop. 您没有使用while循环中设置的price price is set to 120 or 150 but then overwritten by the next value (previous value is completely ignored). price设置为120或150,但随后被下一个值覆盖(先前的值被完全忽略)。 So once your code gets out of the while loop, the latest price set is used and multiplied by total nights. 因此,一旦您的代码退出了while循环,就会使用最新的price集并将其乘以总住宿天数。

So what your code is doing is taking the price of the last day (2/1/2016 in this case) and multiplying it by total nights. 因此,您的代码正在执行的是获取最后一天的price (在这种情况下为2016年2月1日),然后乘以总住宿天数。 What it should be doing is keeping a running total of price inside the loop. 它应该做的是将总price保持在循环内。

You're not doing anything with the price . 你没有做与任何price

It should be used in the while loop. 它应该在while循环中使用。

Also, you should be using .TotalDays , not .Days . 另外,您应该使用.TotalDays ,而不是.Days

Something like: 就像是:

            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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM