在 Processing 3 问题中创建二进制数数组

Creating a array of binary numbers in Processing 3 issue

我不明白为什么我的代码不起作用...

int n = 1023;

int[] a = new int[n];

for (int i = 0; i < n; i++) {
    a[i] = binary(i,10);             //problem line
}

我一直收到错误提示 "cannot convert from String to int" 但我看不到字符串的来源。我只是想制作一个数组,其中每个元素本身都是二进制的。

[0] = 000000000
[1] = 000000001
[2] = 000000010
[3] = 000000011
[4] = 000000100
etc...

感谢您的帮助。

binary()函数returns一个String值。来自参考:

Converts an int, byte, char, or color to a String containing the equivalent binary notation. For example, the color value produced by color(0, 102, 153, 255) will convert to the String value "11111111000000000110011010011001".

说你想要一个二进制数数组是没有意义的。数字只是数字。它们是二进制、十六进制还是十进制只有当你想 显示 这些数字时才重要,这就是为什么 binary() 函数 returns a String值。

您需要将您的值存储为表示二进制表示法的 String 值(然后在需要时使用 unbinary() 函数将它们转换为 int),或者您需要将它们存储为常规 int 值,并且仅在需要显示它们时才使用 binary() 函数。