gpt4 book ai didi

C Turtle 图形不需要的数组输出

转载 作者:行者123 更新时间:2023-11-30 14:49:25 25 4
gpt4 key购买 nike

试图完成一项作业,基本上要求我们创建一个 turtle 图形程序。程序将接受命令,然后在最后处理命令。因此,我想使用循环将命令扫描到数组中,然后通过遍历同一数组并处理命令来处理命令。

int i = 0, j;
int command = 0;
int floor[50][50];
int arrCommand[500];
int valCommand = arrCommand[i--];
int valFloor = floor[floorSize][floorSize];

for (i = 0; i < 50; i++) {
arrCommand[i] = 0;
}

do {
printf("Please enter a command:\n");
printf("[1] - Pen Up\n");
printf("[2] - Pen Down\n");
printf("[3] - Turn Right\n");
printf("[4] - Turn Left\n");
printf("[5],[i] - Move forward i places\n");
printf("[6] - Print current 50x50 frame\n");
printf("[9] - End Program\n");

//loop to initialize commands into array
for (i = 0; arrCommand[i - 1] != 9; i++) {

scanf("%d", &arrCommand[i]);
printf("%d\n\n", arrCommand[i]);
if (arrCommand[i] == 5) {
scanf(",%d", &arrCommand[i]);
}
}

for ( i = 0; i < 25; i++)
{
printf("%d", arrCommand[i]);
}

} while (command != 9);

您可以忽略 do 循环。

用户输入命令后,下一个循环是显示数组中的命令序列。为什么 23469 显示为一个元素,而 -858993460 显示为下一个元素?我希望它继续打印输入的命令。

Please enter a command:
[1] - Pen Up
[2] - Pen Down
[3] - Turn Right
[4] - Turn Left
[5],[i] - Move forward i places
[6] - Print current 50x50 frame
[9] - End Program
2
2

3
3

4
4

6
6

9
9

23469-858993460-858993460-858993460-858993460-858993460-858993460-858993460-
858993460-858993460-858993460-858993460-858993460-858993460-858993460-
858993460-
858993460-858993460-858993460-858993460-858993460Please enter a command:
[1] - Pen Up
[2] - Pen Down
[3] - Turn Right
[4] - Turn Left
[5],[i] - Move forward i places
[6] - Print current 50x50 frame
[9] - End Program

最佳答案

嗯,有一件事

for (i = 0; arrCommand[i - 1] != 9; i++) {

i为0时将不起作用,因为您将尝试访问arrCommand[-1]

关于C Turtle 图形不需要的数组输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49604680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com