一次配对2个数组的元素

漫画大师

新成员
已加入
2016年5月5日
留言内容
2
编程经验
Beginner
您好,
我正在进行夹具项目并尝试互相分配福彩12选5走势图,我有18个福彩12选5走势图,其中一半在家里玩,另一半在远处玩,我创建了2个结构,第一个结构命名为"Teams"并保存有关每个福彩12选5走势图的信息(代码在下面)
C#:
public 结构 队伍                        
        {
            public String name;                    
            public int[] day;                     //It's match day
            public int[] dayCount;                //Holds how much time the theam played match at days
            public bool isMatching;      
            public bool TeamPlace;                 //true=plays at home - false plays at away
            public bool isUefa;                    //Is team plays at Uefa
            public bool isCL;                      //Is team plays at CL
            public int extraMatchDay;              
            public int extraMatchWeek;             
            public bool extraMatchInfo;          //If team has extra match?
            public int[] rivals;                //holds team's previous rivals

            public void CreateArray()
            {
                day = new int[34];
                dayCount = new int[4];  //0->pzt  1->cuma   2->cmt   3->pzr
                rivals = new int[17];
                extraMatchWeek = 1; 

                for (int i = 0; i < rivals.Length; i++)
                {
                    rivals[i] = -1;
                }
            }

            public void NULL()
            {
                name = null;
                TeamPlace = false;
                isUefa = false;
                isCL = false;
                Array.Clear(day, 0, day.Length);  //clear the day 数组
            }
        }

第二个结构被命名"matchedTeams"并保存有关比赛的信息。(代码在下面)

C#:
public 结构 匹配的福彩12选5走势图  //holds competitions
        {
            public 队伍 福彩12选5走势图1;
            public 队伍 福彩12选5走势图2;
            public int MatchDayDate; 

            public void CreateArrayFromTeams() //the objects from 队伍 结构 which uses the function thet "CreateArray" from "Teams" 结构 encountered the NULLException error so I had to create them again.
            {
                福彩12选5走势图1.CreateArray();
                福彩12选5走势图2.CreateArray();
            }

            public void NULLmatched()
            {
                福彩12选5走势图1.NULL();
                福彩12选5走势图2.NULL();
                MatchDayDate = 0;

            }

            public void EqualTheDays(int i)
            {

                福彩12选5走势图1.day[i] = this.MatchDayDate;
                福彩12选5走势图2.day[i] = this.MatchDayDate;
            }
        }

然后我将福彩12选5走势图分离并分配给2个阵列。"Home" and "Away"阵列,并从这2个阵列中分配2个福彩12选5走势图(每个阵列1个福彩12选5走势图)来匹配福彩12选5走势图"team1" and "team2"但是我希望每个福彩12选5走势图互相配对一次,我尝试过但失败了,我的代码如下:

C#:
public static void ShuffleTeamsWithoutWeek(int weekDay, int[] randomHomeNumArray, int[] randomAwayNumArray, 匹配的福彩12选5走势图[,] weeklyMatched, 匹配的福彩12选5走势图[] matchTeams, 队伍[] home, 队伍[] away, 队伍[] teams)
        {

            int randomHome; //It indicates the element which will be chosen from 首页 Array
            int randomAway; ////It indicates the element which will be chosen from 远 Array

            Random randomNum = new Random();

            for (int j = 0; j < weekDay; j++)
            {
                for (int i = 0; i < weeklyMatched.GetLength(1); i++)
                {
                    weeklyMatched[j, i].CreateArrayFromTeams(); //weeklyMatched is a 2 dimensional 数组 instantiated from 匹配的福彩12选5走势图 结构 and it's size is weeklyMatched[34,9]
                }
            }

            for (int week = 0; week < weekDay; week++)
            {

                if (week < weekDay / 2)
                {

                    for (int i = 0; i < 9; i++)   //It equals the elements of 2 数组s to -1
                    {
                        randomHomeNumArray[i] = -1;   //Holds the random numbers for 首页 数组
                        randomAwayNumArray[i] = -1;  //Holds the random numbers for 远 数组
                    }

                    for (int homeArrayCounter = 0; homeArrayCounter < randomHomeNumArray.Length; homeArrayCounter++)
                    {
                        randomHome = randomNum.Next(home.Length);    

                        if (!randomHomeNumArray.Contains(randomHome))
                        {
                            randomHomeNumArray[homeArrayCounter] = randomHome;   
                            //Console.WriteLine(homeArrayCounter + ". iterasyon in 首页 " + randomHomeNumArray[homeArrayCounter]); 
                        }
                        else
                        {
                            homeArrayCounter--;
                        }
                    }
                    for (int awayArrayCounter = 0; awayArrayCounter < randomAwayNumArray.Length; awayArrayCounter++) 
                    {
                        randomAway = randomNum.Next(randomAwayNumArray.Length);    

                        if (!randomAwayNumArray.Contains(randomAway))
                        {
                            randomAwayNumArray[awayArrayCounter] = randomAway;     
                        }
                        else
                        {
                            awayArrayCounter--;
                        }
                    }
                    int RivalCounter = 1;

                    for (int c = 0; c < 9; c++) //There are 9 matches each week
                    {
                        if (weeklyMatched[week, c].team1.isMatching == false)  //choose a team from unchosen ones
                        {
                            weeklyMatched[week, c].team1 = home[randomHomeNumArray[c]];

                            weeklyMatched[week, c].team1.isMatching = true;
                        }

                        if (weeklyMatched[week, c].team2.isMatching == false)   choose the second team from unchosen ones
                        {
                            weeklyMatched[week, c].team2 = away[randomAwayNumArray[c]];

                            if (weeklyMatched[week, c].team1.rivals[RivalCounter] != Array.IndexOf(teams, (weeklyMatched[week, c].team2))) //if team 1 didn't encountered with 福彩12选5走势图2 before
                            {
                                weeklyMatched[week, c].team1.rivals[RivalCounter] = Array.IndexOf(teams, (weeklyMatched[week, c].team2));//assign team as 福彩12选5走势图2
                                weeklyMatched[week, c].team2.isMatching = true; 

                            }
                            else {
                                RivalCounter--; //I'm not sure about this line
                            }
                            RivalCounter++;
                        }
                    }
                }
            }
        }

输出在这里:
输出C#.png

我也有另一个问题。我创建了从创建的对象数组"Teams"通过对所有18个福彩12选5走势图分别使用CreateArray方法构建结构。但是当我尝试将这些福彩12选5走势图分配给MatchTeams对象并尝试使用这些创建的数组时,"NULLException"错误。如何解决?


感谢您的帮助!
 

金西尼

C#论坛主持人
工作人员
已加入
2011年4月23日
留言内容
3,492
地点
悉尼,澳大利亚
编程经验
10+
那么,您是说每对球队应该只互相比赛一次还是在每个球队的主场比赛一次,总共两次?我认为是后者,但我想确认一下。
 

漫画大师

新成员
已加入
2016年5月5日
留言内容
2
编程经验
Beginner
那么,您是说每对球队应该只互相比赛一次还是在每个球队的主场比赛一次,总共两次?我认为是后者,但我想确认一下。

是的,我想一劳永逸地配对。(将有17周的时间,每个福彩12选5走势图必须互相比赛一次。)
 
最佳 底部