Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我的增量计算不正常_C#_Asp.net - Fatal编程技术网

C# 我的增量计算不正常

C# 我的增量计算不正常,c#,asp.net,C#,Asp.net,好的,按下一个按钮,我的代码应该增加系泊1,直到达到5,在达到5后,它必须增加桥墩1,然后增加系泊1,直到达到5,然后增加桥墩1,依此类推 但它现在做的是,它增加桥墩,然后系泊到5,但在我点击按钮让它再次增加(在这里它应该增加桥墩到2,系泊回到1)之后,它崩溃了 这是我的密码 //Setting the max value for Piers and Moorings public const int maxPiers = 6; public const int

好的,按下一个按钮,我的代码应该增加系泊1,直到达到5,在达到5后,它必须增加桥墩1,然后增加系泊1,直到达到5,然后增加桥墩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;
        }
这就是“船的反应”

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.

看起来很清楚-您试图将
系泊
的值指定为6,而该值仅从1变为5

看起来你的
码头
系泊系统
回路是向后的

//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++)
//如果mooringCalc设置为0,如果小于maxMoorings增量
对于(int-pierCalc=0;pierCalc
方法中的外循环有一个条件

mooringCalc < maxMoorings
6
出现异常,因为枚举只有5项。因此出现错误

“6”不是NewBoatBookingApp.系泊的有效值


看起来你正在以一种非常复杂的方式处理这个问题

如果我没弄错的话,你们有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)
        };
    }
//设置桥墩和系泊系统的最大值
mooringsPerPier的公共常数=5;
公共常数int MAXMOOLINGS=30;
private staticbool[]ReserveMooling=新bool[maxMoorings];
[网络方法]
公共船艇响应储备系泊(船艇请求)
{
int系泊=数组.IndexOf(保留系泊,假);
如果(系泊<0)
{
返回null;
}
保留系泊[系泊]=正确;
返回新的BoatResponse()
{
码头=(码头)(内部(系泊/系泊平台)),
系泊=(系泊)(内部(系泊%mooringsperPier)
};
}

同样,如果您在这里使用这两个枚举,我将丢失它们,除非它们以后会得到有用的名称:)

你已经将<代码> Max MooReS/<代码>设置为30。在<代码>系泊< /代码> EnUM中只有五个选项。每个码头有5个,总共30个。您可能希望在每个码头重置计数,因为在EnUM中只有5个选项:)对我来说不太适合使用枚举。您可能需要考虑使用类而不是枚举。。有一个pier类和一个系泊类。然后,每个pier都可以有一个系泊列表以及您可能需要的其他属性。例如,您不能将位置数据添加到您拥有的解决方案中的pier或任何属性中。然后,piers的实例可以存储在一个列表中,该列表将更易于管理。正如我在上面所评论的,在e每个桥墩有5个系泊点,有6个桥墩,不可能不超过5个吗?每个桥墩最多只有5个系泊点。我的第二个回路应该从0到5(30/6)。这修复了它,谢谢,它现在正确增加了。我会在可能的时候给出正确的答案
res.Mooring = (Mooring)mooringCalc; //here mooringCalc is 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)
        };
    }