嵌套的循环变量增量

时间:2018-07-17 18:14:44

标签: c# loops for-loop nested 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草图并上传以方便使用的程序。

任何帮助将不胜感激!

干杯, 摩根

2 个答案:

答案 0 :(得分:1)

我认为您的第二个循环应该是

for (i = (x * columns); i < ((x + 1) * columns); i++)

第一次迭代将是0、1、2、3、4,第二次迭代将是5、6、7、8、9,依此类推。

答案 1 :(得分:0)

如果跳转始终是这种情况,则只需进行跳转

 int i;
        for(int x = 0; x < rows; x++) //row
        {
            string buttonbyte = "{";
            for (i = 0; i < columns; i++) //column
            {
                buttonbyte += (i + columns*x); // collum + collumns* row
                buttonbyte += ",";
            }
            sketch[9 + x] = buttonbyte + "},";

        }

PS:这可能不起作用,因为我现在没有编译器,您可能需要将(i +列* x)转换为字符串

相关问题