我的增量计算无法正常工作

时间:2013-10-28 13:06:49

标签: c# asp.net

好吧按下按钮,我的代码应该将系泊增加1,直到它达到5,在它达到5之后,它必须将码头增加1,然后系泊1,直到它得到至五,然后将码头增加一个然后再增加。

但它目前的作用是,它将码头增加,然后将系泊增加到5,但是在我点击按钮后再次增加(它应该将码头增加到2并且系泊回到1)它崩溃了。

这是我的代码

//Setting the max value for Piers and Moorings
        public const int maxPiers = 6;
        public const int maxMoorings = 30;
        private static bool[,] reserveMooring = new bool[maxPiers, maxMoorings];

[WebMethod]
        public BoatResponse ReserveMooring(BoatRequest req)
        {
            var res = new BoatResponse();

            //if mooringCalc set to 0, if smaller than maxMoorings increment
            for (int mooringCalc = 0; mooringCalc < maxMoorings; mooringCalc++)
            {
                //if pierCalc set to 0, if smaller than maxPiers increment
                for (int pierCalc = 0; pierCalc < maxMoorings / maxPiers; mooringCalc++)
                {

                    if (!reserveMooring[pierCalc, mooringCalc])
                    {
                        reserveMooring[pierCalc, mooringCalc] = true;
                        res.Pier = (Pier)pierCalc;
                        res.Mooring = (Mooring)mooringCalc;
                        return res;
                    }
                }
            }
            return null;
        }

崩溃发生在这里:

private void button1_Click(object sender, EventArgs e)
        {
            var req = new BoatRequest();
            req.Name = txtName.Text;
            var client = new BoatReserveSoapClient();
            BoatResponse response = client.ReserveMooring(req);//error is on this line

这是BoatResponse

namespace NewBoatBookingApp
{

    public enum Pier
    {
        one,
        two,
        three,
        four,
        five,
        six
    }


    public enum Mooring
    { 
        one,
        two,
        three,
        four,
        five
    }

    public class BoatResponse
    {
        public Pier Pier;
        public Mooring Mooring;
    }
}

这是错误:

System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: Instance validation error: '6' is not a valid value for NewBoatBookingApp.Mooring.

3 个答案:

答案 0 :(得分:4)

看起来非常清楚 - 当你只从1到5时,你试图分配一个值为6的Mooring

看起来您的PiersMoorings循环是向后的?

//if mooringCalc set to 0, if smaller than maxMoorings increment
for (int pierCalc = 0; pierCalc < maxPiers; pierCalc++)
{
    //if pierCalc set to 0, if smaller than maxPiers increment
    for (int mooringCalc = 0; mooringCalc < maxMoorings / maxPiers; mooringCalc++)

答案 1 :(得分:1)

您方法中的外循环具有条件

mooringCalc < maxMoorings

您将maxMoorings设置为30并且您的枚举只有6个值。所以当你这样做时:

res.Mooring = (Mooring)mooringCalc; //here mooringCalc is 6

您获得值6的例外,因为枚举只有5个项目。因此错误

  

'6'不是NewBoatBookingApp.Mooring的有效值。

答案 2 :(得分:0)

看起来你正以相当复杂的方式接近这个......

如果我理解正确,你有30个系泊设备,它们位于1到6个码头。

为什么你不只是将你的系泊设备视为一个连续的设备,找到一个空的设备,然后计算一个是哪个码头?

//Setting the max value for Piers and Moorings
    public const int mooringsPerPier = 5;
    public const int maxMoorings = 30;
    private staticbool[] reserveMooring = new bool[maxMoorings];

[WebMethod]
    public BoatResponse ReserveMooring(BoatRequest req)
    {
        int mooring = Array.IndexOf(reserveMooring, false);
        if (mooring < 0)
        {
          return null;
        }
        reserveMooring[mooring] = true;
        return new BoatResponse()
        {
           Pier = (Pier)(int(mooring / mooringsPerPier)),
           Mooring = (Mooring)(int(mooring % mooringsperPier)
        };
    }

同样,我会在这里使用它们的方式丢失两个枚举,除非它们稍后会得到有用的名称:)