外部范围时未定义变量

编写一个程序,让用户抛出五个骰子并以“图形方式”显示结果屏幕。
该程序应该首先通过填充一个介于1之间的5个数字的数组来模拟五个掷骰子然后,函数应该通过在屏幕上显示字符和计算函数来“绘制”结果总和。
我得到第一个函数的错误消息,它说我没有定义矩阵,我在“if”中定义了。

#include  int sumOfDie(int inputArray[], int arraySize); int drawDie(int inputArray[], int arraySize) { int i, row, column=0; for (i=0; i<arraySize; i++) //determine the graphic number from the random number { if (inputArray[i]==1) { char matrix [3][4] = {{" "},{" * "},{" "}}; } if (inputArray[i]==2) { char matrix [3][4] = {{"* "},{" "},{" *"}}; } if (inputArray[i]==3) { char matrix [3][4] = {{"* "},{" * "},{" *"}}; } if (inputArray[i]==4) { char matrix [3][4] = {{"* *"},{" "},{"* *"}}; } if (inputArray[i]==5) { char matrix [3][4] = {{"* *"},{" * "},{"* *"}}; } for (row=0; row<3; row++) //Print out the matrix { for(column=0; column<4; column++) { printf("%c ", matrix[row][column]); } printf("\n"); } } } int sumOfDie(int inputArray[], int arraySize) { int i, sum=0; for (i=0; i<arraySize; i++) { sum=sum+inputArray[i]; } return sum; } int main(void) { int i; int inputArry[5]; srand(time(NULL)); for(i=0; i<5; i++) { inputArry[i] = rand()%5+1; } for (i=0; i<5; i++) { printf("Number:%d\n", inputArry[i]); } drawDie(inputArry, 5); sum = sumOfDie(inputArray,5) printf("The sum of %i + %i + %i + %i + %i = %i", inputArry[0], inputArry[1], inputArry[2], inputArry[3], inputArry[4], sum); return 0; } 

在函数drawDie ,名为matrix的每个变量的范围仅限于声明它们的if语句,以便以后不能使用它们进行打印。

您可以在单个多维数组中收集表示骰子所需的所有字符串,然后打印您需要的字符串。

这是一种可能的实施方式(考虑六面骰子):

 #include  void print_n_times_in_a_row(const char *str, int n) { for ( int i = 0; i < n; ++i ) { printf(" %s", str); } puts(""); } void draw_dices(int* values, int n) { static const char dice_str[][3][8] = { {{" "},{" * "},{" "}}, // 1 {{" * "},{" "},{" * "}}, // 2 {{" * "},{" * "},{" * "}}, // ... {{" * * "},{" "},{" * * "}}, {{" * * "},{" * "},{" * * "}}, {{" * * "},{" * * "},{" * * "}} // 6. Just in case... }; // I'll print all the "dices" in a row print_n_times_in_a_row("+-------+", n); for ( int j = 0; j < 3; ++j ) { for ( int i = 0; i < n; ++i ) { printf(" |%s|", dice_str[values[i] - 1][j]); } puts(""); } print_n_times_in_a_row("+-------+", n); } int main(void) { int dices[] = {4, 2, 5, 6, 1, 3}; draw_dices(dices, 6); } 

哪个输出:

  + ------- + + ------- + + ------- + + ------- + + ------- + + ---- --- +
  |  * * |  |  * |  |  * * |  |  * * |  |  |  |  * |
  |  |  |  |  |  * |  |  * * |  |  * |  |  * |
  |  * * |  |  * |  |  * * |  |  * * |  |  |  |  * |
  + ------- + + ------- + + ------- + + ------- + + ------- + + ---- --- +

你不应该声明条件数组,即使总是创建数组,你应该在if语句char matrix [3][4];之前声明你的数组char matrix [3][4];

离开if范围后,该变量不再存在

使代码工作的解决方案就是这样,你可以使用一个开关盒来区分你想在屏幕上打印什么。 该array仅存储要在交换机中使用的索引。

你做错了是关于你在if语句范围内声明array地方,所以,一旦执行了块ifarray就不再存在了。

另外,关于如何在char array中存储数据的想法,你需要事先指定边(在运行时也可以在C99中跳过),或者动态分配它(这里不需要) 。

因此, char var可以存储单个字节,但是您尝试存储字符串{" "} ,因此您会收到错误。

 #include  #include  #include  int sumOfDie(int inputArray[], int arraySize); int drawDie(int inputArray[], int arraySize) { int i, row, column=0; char matrix[5][5]; for (i=0; i