问题是我需要找到每个星期六和星期五晚上,并在星期五或星期六向他们收费140美元。我应该使用while循环吗?
DateTime ArrivalDate = DateTime.Parse(txtArrivalDate.Text);
DateTime DepartureDate = DateTime.Parse(txtDepartureDate.Text);
TimeSpan numberOfNights = DepartureDate.Subtract(ArrivalDate);
decimal nights = numberOfNights.Days;
txtNights.Text = nights.ToString();
//txtTotalPrice
decimal pricePerNight = 120.00m;
decimal totalNight = nights * pricePerNight;
txtTotalPrice.Text = "$" + totalNight.ToString();
txtAvgPrice.Text = "$" + pricePerNight.ToString();
// finds the fridays
decimal morePricePerNight = 140.00m;
int i = 0;
// while loop
while (ArrivalDate.DayOfWeek != DayOfWeek.Friday)
{
ArrivalDate = ArrivalDate.AddDays(1);
}
while (DepartureDate.DayOfWeek != DayOfWeek.Friday)
{
DepartureDate = DepartureDate.AddDays(1);
}
答案 0 :(得分:1)
为什么不只使用一个while循环?
我想您可以编写以下代码:
var date = DepartureDate;
while (date < ArrivalDate)
{
if (date.DayOfWeek == DayOfWeek.Friday || date.DayOfWeek == DayOfWeek.Saturday)
{
totalNight += 140.00m;
}
date = date.AddDays(1);
}