微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当我的索引是变量时无法访问数组值

如何解决当我的索引是变量时无法访问数组值

我在访问存储在二维数组中的值时遇到问题...

当我将索引写为数字时,它可以工作,但是当我的索引是一个变量(其中包含一个数字)时,它不起作用...

这是我的代码

#include <avr/pgmspace.h>

const char eQ1[] PROGMEM = {"En moyenne,combien du temps de notre sommeil passons nous à rêver ? \n 1 -> 10% \n 2 -> 20% \n 3 -> 30% \n 4 -> 40%"};
const char eQ2[] PROGMEM = {"Uranus est proche de : \n 1 -> Mercure \n 2 -> Jupiter \n 3 -> Neptune"};
const char eQ3[] PROGMEM = {"Quelle plante à fleur jaune sert à fabriquer un biocarburant ? \n 1 -> L'arnica \n 2 -> Le colza \n 3 -> Le chrysanthème \n 4 -> Le crocus"};
const char eQ4[] PROGMEM = {"Le paon blanc est un paon albinos: \n 1 -> Vrai \n 2 -> Faux"};
const char eQ5[] PROGMEM = {"Quelle roche transforme-t-on pour fabriquer du plâtre ? \n 1 -> Le gypse \n 2 -> L’argile \n 3 -> Le sable"};
const char eQ6[] PROGMEM = {"Je suis le fruit de l’anacardier. On me grignote à l’apéritif. Qui suis-je ? \n 1 -> La pistache \n 2 -> L’amande \n 3 ->La noix de cajou"};
const char eQ7[] PROGMEM = {"Un arbre peut rejeter plus de 200 litres d’eau dans l’atmosphère en 1h : \n 1 -> Vrai \n 2 -> Faux"};
const char eQ8[] PROGMEM = {"Le bourdon terrestre construit son nid : \n 1 -> Dans les arbres \n 2 -> Sous la terre\n 3 -> Dans les cheminées \n 4 -> Sur l’eau"};
const char eQ9[] PROGMEM = {"A partir de quelle céréale produit-on le saké,un alcool japonais  ? \n 1 -> Le blé \n 2 -> L’avoine\n 3 -> Le riz \n 4 -> Le seigle"};
const char eQ10[] PROGMEM = {"La couleur de l’hortensia dépend de l’acidité du sol : \n 1 -> Vrai \n 2 -> Faux"};
const char eQ11[] PROGMEM = {"Le saucissonier est un arbre : \n 1 -> Vrai \n 2 -> Faux"};


const char * const easyQuestions[11][2] PROGMEM = {
  {eQ1,"2"},{eQ2,"3"},{eQ3,{eQ4,{eQ5,"1"},{eQ6,{eQ7,{eQ8,{eQ9,{eQ10,{eQ11,"1"}
};

int questionsIndex[4] = {};


void setup(){

  // Fill the array with different values

  questionsIndex[0] = random(1,11);
  questionsIndex[1] = random(1,11);

  while (questionsIndex[1] == questionsIndex[0]) {
    questionsIndex[1] = random(1,11);
  }

  questionsIndex[2] = random(1,11);

  while (questionsIndex[2] == questionsIndex[0] || questionsIndex[2] == questionsIndex[1]) {
    questionsIndex[2] = random(1,11);
  }

  questionsIndex[3] = random(1,11);

  while (questionsIndex[3] == questionsIndex[0] || questionsIndex[3] == questionsIndex[1] || questionsIndex[3] == questionsIndex[2]) {
    questionsIndex[3] = random(1,11);
  }

}

void loop(){

   Serial.print(questionsIndex[0]); // This always returns a different number
   Serial.print(easyQuestions[questionsIndex[0]][1]);
   Serial.println();

/* if I write :
   Serial.print(easyQuestions[3][1]);
   It prints the matching value...
*/

}

我还尝试将随机索引存储到一个变量中并将此变量用作索引,但它也不起作用......

我是一名学生,这是一个学校项目......我对 Arduino 很陌生

提前致谢!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。