对于循环,不正确地索引索引

发布于 2025-01-30 10:52:23 字数 658 浏览 5 评论 0原文

我想在数组中存储键盘的输入,但是我的循环似乎无法正常工作。该程序没有从键盘读取键盘的输入,而是将其添加到数组中,然后递增索引,而是继续打印奇怪的索引。

代码

    void loop(){
  char arr[3];

  for (int i = 0; i<3; i++){
      char input = customKeypad.getKey();
      if (input != NO_KEY){
        do{
             arr[i] = input;
             Serial.println("Index");
             Serial.println(i);
             Serial.println("Value");
             Serial.println(arr[i]);
          } while(input == NO_KEY);
      }
  }
}

控制台输出

Index
2
Value
1
Index
1
Value
2
Index
1
Value
3
Index
1
Value
4
Index
2
Value
5
Index
2
Value
6

I'd like to store input from keypad in array, but my for loop doesn't seem to work properly. Instead of reading an input from keypad, adding it to the array, and then incrementing the index, the program keep printing weird indexes.

Code

    void loop(){
  char arr[3];

  for (int i = 0; i<3; i++){
      char input = customKeypad.getKey();
      if (input != NO_KEY){
        do{
             arr[i] = input;
             Serial.println("Index");
             Serial.println(i);
             Serial.println("Value");
             Serial.println(arr[i]);
          } while(input == NO_KEY);
      }
  }
}

Console output

Index
2
Value
1
Index
1
Value
2
Index
1
Value
3
Index
1
Value
4
Index
2
Value
5
Index
2
Value
6

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ㄟ。诗瑗 2025-02-06 10:52:23

这是您程序的数据流。这可能是一个评论,但我需要可视化以使其更容易理解。目前尚不清楚您要做什么:

“ alt =”“在此处

input==NO_KEY
input!=NO_KEY
input!=NO_KEY

net/ nu9vp.png 可以覆盖数组的值。这是你想要的吗?

另请注意,While循环永远不会重复,这从数据流中很明显。

我已经重写了您的程序要在Arduino之外进行测试:

#include "stdio.h"

#define NO_KEY 'a'

int main(){
    while(1){

      char arr[3];

      for (int i = 0; i<3; i++){
          char input;
          printf("%d -> ", i);
          scanf(" %c",&input);
          if (input != NO_KEY){
            do{
                 arr[i] = input;
              } while(input == NO_KEY );
          }

      }
      for (int i = 0; i <3; i++){   printf("Value %d\n",arr[i]); }

      printf("\n Reset\n");
    }
}

这就是输出,如您的代码所预期

0 -> a
1 -> b
2 -> c
Value 127
Value 98
Value 99

 Reset
0 -> b
1 -> a
2 -> a
Value 98
Value 98
Value 99

This is the dataflow of your program. This probably could be a comment but I needed the visualization to make it more understandable. It is not really clear what you want to do:

enter image description here

For instance, if you enter, in this order:

input==NO_KEY
input!=NO_KEY
input!=NO_KEY

You update the indexes #1 and #2, then the iteration (arduino loop) restarts and you can overwrite the values of the array. Is this what you want?

Also notice that the while loop never gets repeated, this is very clear from the dataflow.

I have rewritten you program to be tested outside of Arduino:

#include "stdio.h"

#define NO_KEY 'a'

int main(){
    while(1){

      char arr[3];

      for (int i = 0; i<3; i++){
          char input;
          printf("%d -> ", i);
          scanf(" %c",&input);
          if (input != NO_KEY){
            do{
                 arr[i] = input;
              } while(input == NO_KEY );
          }

      }
      for (int i = 0; i <3; i++){   printf("Value %d\n",arr[i]); }

      printf("\n Reset\n");
    }
}

And this is the output, as expected from your code

0 -> a
1 -> b
2 -> c
Value 127
Value 98
Value 99

 Reset
0 -> b
1 -> a
2 -> a
Value 98
Value 98
Value 99

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文