超出范围时未定义变量 [英] Variable not defined when outside scope
问题描述
编写一个程序,该程序可以让用户掷出五个骰子并以图形方式显示结果在屏幕上.
程序应通过模拟5个掷骰子来开始,并通过在1个之间的5个数字填充数组来开始和5.函数应通过在屏幕上显示字符以及计算函数来``绘制''结果总和.
我收到第一个函数的错误消息,它说我尚未定义矩阵,该矩阵是在"if"中定义的.
Writing a program that lets the user throw five dice and to display the result "graphically" on the screen.
The program should start by simulating five die throws by filling an array with 5 numbers between 1 and 5. A function should then "draw" the result by displaying characters on the screen and a function who calculate the sum.
I get a error message the first function, it says I have not defined the matrix, which I defined in the "if".
#include <stdio.h>
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
语句,因此它们以后将无法使用来打印.
In the function drawDie
, the scope of each of the variables named matrix
is limited to the if
statement where they are declared, so that they can't be used later to be printed.
您可以在单个多维数组中收集表示骰子所需的所有字符串,然后打印所需的字符串.
You can collect all the strings needed to represent the dices in a single multidimentional array and then print the ones you need.
这是一种可能的实现方式(考虑有六个骰子):
This is a possible implementation (considering a six sided dice):
#include <stdio.h>
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);
}
哪个输出:
+-------+ +-------+ +-------+ +-------+ +-------+ +-------+
| * * | | * | | * * | | * * | | | | * |
| | | | | * | | * * | | * | | * |
| * * | | * | | * * | | * * | | | | * |
+-------+ +-------+ +-------+ +-------+ +-------+ +-------+
这篇关于超出范围时未定义变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!