c-这是访问数组元素的正确方法吗



我有一个充满1和0的数组,还有两个LED(1=左LED/0=右LED(

int Game[100]

我有一个数组,我在其中编写用户操作(使用操纵杆(。((这个阵列最初的想法是不断地将其大小增加一。。。但我读到你不能这样做,你需要实现一个"链表",但现在,好吧。(

int Player[2]

我想遍历数组"的所有元素;"游戏";,但仅通过一个元件

int i;
for(i = 0; i <= 100; i++) {
if(Game[i] == 1) {
// Left LED ON
// Left LED OFF
}
else if (Game[i] == 0) {
// Right LED ON
// Right LED ON
}

我的意思是,首先我想从阵列中取出两个元素,打开LED。。。等待用户输入。。。然后取另一个元素(重复前两个(,依此类推。有什么方法可以做到这一点吗?

我不确定你的确切动机是什么,但我认为我可以帮助你根据用户输入逐个访问元素。检查我的代码:(希望你能找到有用的东西(

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int Game[100];
int i,j=0;
//filling each element with 1s and 0s
for(i=0;i<100;i++)
{
if(i%2==0)
Game[i]=0;
else
Game[1]=1;
}
i=0;
printf("User Input(-1 to Quit): ");
scanf("%d",&j);
while(j!=-1 && i<100)
{
//your code
if(Game[i] == 1)
{
// Left LED ON
// Left LED OFF
}
else
{
// Right LED ON
// Right LED ON
}
++i;
printf("Another User Input(-1 to quit): ");
scanf("%d",&j);
}
return 0;
}

最新更新