嵌套 For 循环变量增量
Nested For Loop Variable Increment
我有一个文本框显示软件编辑的草图,变量是动态变化的。
https://cdn.discordapp.com/attachments/398780553731506176/468835565304020995/unknown.png
但是对于按钮数组,我需要使用一些for循环来写入数字
格式需要这样
byte buttons[NUMROWS][NUMCOLS] = {
{0,1,2,3,4,},
{5,6,7,8,9,},
{10,11,12,13,14,},
但我目前所能做到的只是
byte buttons[NUMROWS][NUMCOLS] = {
{0,1,2,3,4,},
{1,2,3,4,5,},
{2,3,4,5,6,},
我需要推进循环以使数字增加。我正在使用两个嵌套的 for 循环
int i;
for(int x = 0; x < rows; x++) //row
{
string buttonbyte = "{";
for (i = x; i < columns + x; i++) //column
{
buttonbyte += i;
buttonbyte += ",";
}
sketch[9 + x] = buttonbyte + "},";
}
该代码用于编辑 arduino 的程序。ide 草图并上传,以便于使用。
如有任何帮助,我们将不胜感激!
干杯,
摩根
如果跳转总是这样,你只需要做到
int i;
for(int x = 0; x < rows; x++) //row
{
string buttonbyte = "{";
for (i = 0; i < columns; i++) //column
{
buttonbyte += (i + columns*x); // column + columns * row
buttonbyte += ",";
}
sketch[9 + x] = buttonbyte + "},";
}
PS:这可能行不通,因为我现在没有编译器,您可能需要将 (i + columns*x) 转换为字符串
我认为你的第二个循环需要
for (i = (x * columns); i < ((x + 1) * columns); i++)
第一次迭代为 0,1,2,3,4,第二次为 5,6,7,8,9,依此类推。
我有一个文本框显示软件编辑的草图,变量是动态变化的。
https://cdn.discordapp.com/attachments/398780553731506176/468835565304020995/unknown.png
但是对于按钮数组,我需要使用一些for循环来写入数字
格式需要这样
byte buttons[NUMROWS][NUMCOLS] = {
{0,1,2,3,4,},
{5,6,7,8,9,},
{10,11,12,13,14,},
但我目前所能做到的只是
byte buttons[NUMROWS][NUMCOLS] = {
{0,1,2,3,4,},
{1,2,3,4,5,},
{2,3,4,5,6,},
我需要推进循环以使数字增加。我正在使用两个嵌套的 for 循环
int i;
for(int x = 0; x < rows; x++) //row
{
string buttonbyte = "{";
for (i = x; i < columns + x; i++) //column
{
buttonbyte += i;
buttonbyte += ",";
}
sketch[9 + x] = buttonbyte + "},";
}
该代码用于编辑 arduino 的程序。ide 草图并上传,以便于使用。
如有任何帮助,我们将不胜感激!
干杯, 摩根
如果跳转总是这样,你只需要做到
int i;
for(int x = 0; x < rows; x++) //row
{
string buttonbyte = "{";
for (i = 0; i < columns; i++) //column
{
buttonbyte += (i + columns*x); // column + columns * row
buttonbyte += ",";
}
sketch[9 + x] = buttonbyte + "},";
}
PS:这可能行不通,因为我现在没有编译器,您可能需要将 (i + columns*x) 转换为字符串
我认为你的第二个循环需要
for (i = (x * columns); i < ((x + 1) * columns); i++)
第一次迭代为 0,1,2,3,4,第二次为 5,6,7,8,9,依此类推。