满足条件时减少变量 [英] Decrementing variable when condition is met

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

问题描述

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.

推荐答案

我最近编写了一个函数,开始递增一个值然后当达到峰值时,它开始递减,我用它来为页面上的rgb颜色设置动画





I wrote a function recently that starts incrementing a value then when the peak is reached it starts decrementing, i used it to animate rgb colors on page


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


这里没有真正的问题,但我猜你的代码没有按预期工作。

没有你的数据,我们无法设计发生了什么。

唯一的可能是使用调试器并在代码执行时检查变量值。



你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
当代码不做ex的时候你已经接近了一个错误。
There is no real question here, but I guess your code don't work as expected.
Without your data, we can't devise what is going on.
The only possibility is to use the debugger and check the variables values as the code is executing.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆