我正在尝试增加一些数组值:
int counter[] = {0,0,0,0,0,0,0,0};
如果位置 0 的数字值达到 25,则位置 1 的值加 1,位置 0 重置为 0。依此类推 - 当索引位置 2 达到 25 时,位置 3 增加 1,并重置它的值。自身价值为0。
我正在做一些 base26 增量 - 生成给定数量的字母的所有字母组合。理想情况下,我希望它可以无限工作(理论上)——当最后一个值达到 25 时,会附加一个新的数组索引。
我正在研究与上一个问题相关的项目 - 可能会澄清我正在尝试做的事情:每个字母表的排列最多 29 个字符? https://stackoverflow.com/questions/4610093/every-permutation-of-the-alphabet-up-to-29-characters
这是我现在的代码:
// Set the variables.
String neologism;
int counter[] = {0,0,0,0,0,0,0,0};
String base26[] = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
void setup() {
// Initialize serial communication:
Serial.begin(9600);
}
void loop() {
int i = 0;
// Reset or increment the counter.
if (counter[i] == 25) {
counter[i] = 0;
counter[i+1]++;
}
else {
counter[i]++;
}
neologism = letters(counter[i]);
Serial.print(neologism+'\n');
delay(100);
i++;
if(i>7) {
i=0;
}
}
String letters(int counter) {
String newword;
for(int i=0; i <= 7; i++) {
newword += base26[counter];
}
return newword;
}