本文介绍了满足条件时减少变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public void ReserveRoom(List<reservationtype> reservation)
       {
           List<reservationtype> RequestReservation = new List<reservationtype>();

           RequestReservation = reservation;   //List that contains the test cases.

           string reservationid = "0001";
           int number = int.Parse(reservationid);

           int count = 0;

           foreach (ReservationType requestReservation in RequestReservation)
           {
               List<string> DateList = CreateDateList(requestReservation.StartDate, requestReservation.numDays);

               foreach (Inventory inventory in RoomInventory)
               {
                  if (requestReservation.hotelId.Equals (inventory.HotelId))
                   {

                     if (requestReservation.roomType == inventory.RoomType)
                       {

                              for (int i = 0; i < DateList.Count; i++)
                              {
                       if (DateList[i].Equals(inventory.Date) && inventory.Quantity > 0)
                               {

                                   count++;

                                   if (requestReservation.numDays == count)
                                   {
                                       for (int j = 0; j < DateList.Count; j++)
                                       {
                                          if (DateList[j].Equals(inventory.Date))
                                               {
                                                 inventory.Quantity--;

                                               }

                                       }

                                       requestReservation.reservationId = "000" + (number + 1).ToString();
                                       requestReservation.result = ReservationType.ReservationResultType.Success;
                                       count = 0;

                                       foreach (Hotels h in LHotels)
                                       {
                                           foreach (Room rm in h.RoomList)
                                           {
                                               if (requestReservation.hotelId == h.HotelId && requestReservation.roomType == rm.RoomType)
                                               {
                                                   requestReservation.cost = (requestReservation.numDays * rm.Rate);
                                               }

                                           }
                                       }
                                   } // end of if num == counts
                                   else {
                                       requestReservation.reservationId = null;
                                       requestReservation.result = ReservationType.ReservationResultType.RoomNotAvailable;
                                       count = 0;
                                   }

                               } //end of if


                           }//end of forloop

                       }//end of if statement (Roomtypes)

                   }//end of If statement( inventory.hotelID)

               } // end of foreach inventory
               number++;
           }// end of Foreach

           serial = new XmlSerializer(RequestReservation.GetType());
           sw = new StreamWriter(ReservationFilename);
           serial.Serialize(sw, RequestReservation);
           sw.Close();


       }// End of ReserveRoom Method





我尝试过:



1)条件成立后,我做了forloop遍历每个变量,其中datelist [i] == inventory.date并从中扣除1。



2)没有for循环,条件为真后,扣除1,但根本没有变化。



What I have tried:

1)after the condition was true, i did forloop to go through each variables where datelist[i] == inventory.date and deduct 1 from it.

2) without the for loop, after condition was true, deduct 1 , but did not change at all.

推荐答案

var current;
const min = 0;
const max = 255;
var b = true;

    if (b == true) {
        current += 1;
        if (current >= max) {
            b = false;
        }
    }
    else {
        current = current - 1;
        if (current <= min) {
            b = true;
    }



这篇关于满足条件时减少变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 06:40