为什么我的C#代码不能按预期工作(数组转换,新手)

时间:2018-05-14 10:17:39

标签: c# arrays loops

我正在尝试取数组(仅限0或1)并按如下方式重复转换:

  • 第一个和最后一个数字始终为0;
  • 所有其他数字都是 派生自先前的数组状态:if array [n]及其两个邻居 (array [n-1],array [n]和array [n + 1])具有相同的值(三个0或三个1)然后newarray [n]应该是1,否则它应该是0(产生不错)模式)。

例如,如果数组大小为10且以全零开始,则程序应输出:

0000000000
0111111110
0011111100
0001111000
0100110010
0000000000
0111111110
...and so on 

我编写了一个应该执行此操作的代码,但它无法按预期工作。它总是完美地进行第一次转换,但随后开始出现一些错误的,不对称的,疯狂的东西:

0000000000
0111111110
0000000000
0101010100
0000000010
0101010000

是什么让我的代码表现得像它一样?

以下是代码:

namespace test
{
    class Program
    {
        public static void Main(string[] args)
        {
            const int leng = 10; //length of array
            int[] arrayone = new int[leng]; 
            int[] arraytwo = new int[leng];
            for (int i = 0; i<=leng-1; i++)
            {
                arrayone[i] = 0;
                arraytwo[i] = 0;
            } //making two arrays and filling them with zeroes

            for (int i = 0; i<=leng-1; i++)
            {
                Console.Write(arrayone[i]);
            }
            Console.WriteLine(' '); //printing the first state of array


            for (int st=1; st<=16; st++) //my main loop
            {
                arraytwo[0]=0;
                arraytwo[leng - 1]=0; //making sure that first and last elements are zero. I'm not sure I need this since I fill both arrays with zeroes in the beginning. But it probably doesn't hurt?
                for (int i = 1; i<=leng-2; i++) //calculating new states for elements from second to second-to-last
                {
                    if (((arrayone[i-1]) + (arrayone[i]) + (arrayone[i+1]) == 0) | ((arrayone[i-1]) + (arrayone[i]) + (arrayone[i+1]) == 3) == true)
                        arraytwo[i] = 1; 
                    else 
                        arraytwo[i] = 0;
                }
                //by now arraytwo contains transformed version of arrayone

                for (int i = 0; i<=leng-1; i++)
                {
                    Console.Write(arraytwo[i]);

                } //printing result

                arrayone = arraytwo; //copying content of arraytwo to arrayone for the next round of transformation;
                Console.WriteLine(' '); 
            }



            Console.Write(" ");
            Console.ReadKey(true);
        }
    }
}

Tweakable版本:https://dotnetfiddle.net/8htp9N

2 个答案:

答案 0 :(得分:3)

正如你所指出的那样,你正在谈论一个对象并对其进行处理,最后你指的是引用而不是值。打击的一种方法是

代表你的专栏:arrayone = arraytwo; 将其更改为:arrayone = (int[])arraytwo.Clone();

这将复制值 - 对于整数,这就足够了。

答案 1 :(得分:0)

请注意您当前的代码复杂,因此难以调试。使其更简单,提取方法

using System.Linq;

...

private static IEnumerable<int[]> Generate(int width) {
  int[] item = new int[width];

  while (true) {
    yield return item.ToArray(); // .ToArray() - return a copy of the item

    int[] next = new int[width];

    for (int i = 1; i < item.Length - 1; ++i) 
      if (item[i - 1] == item[i] && item[i] == item[i + 1])
        next[i] = 1;

    item = next;
  }
}

然后你可以把

public static void Main(string[] args) {
  var result = Generate(10) // each line of 10 items
    .Take(7) // 7 lines
    .Select(item => string.Concat(item));

  Console.Write(string.Join(Environment.NewLine, result));
}

结果:

0000000000
0111111110
0011111100
0001111000
0100110010
0000000000
0111111110