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

时间:2018-04-02 02:46:07

标签: c arrays turtle-graphics

试图完成一项家庭作业,基本上它要求我们创建一个乌龟图形程序。程序将接受命令,然后在最后处理命令。所以我虽然使用循环将命令扫描到数组中,然后通过遍历相同的数组并处理命令来处理命令。

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

3 个答案:

答案 0 :(得分:1)

嗯,有一件事

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

无法运行,因为您将尝试访问arrCommand[-1]

答案 1 :(得分:0)

你需要像这样改变才能工作,

//loop to initialize commands into array
    for (i = 1; arrCommand[i - 1] != 9; i++) {  // or for (i = 0; arrCommand[i] != 9; i++) Need to change so that it check value from 0 index of arrCommand

        scanf("%d", &arrCommand[i]);
        printf("%d\n\n", arrCommand[i]);
        if (arrCommand[i] == 5) {
            scanf("%d", &arrCommand[i]); //here you need to remove ','
        }
    }

希望它适合你。

答案 2 :(得分:0)

让我们对该计划进行重组。首先,当我们遇到一个&#39;时,我们将存储5命令,它的参数作为arrCommand中的以下值,所以我们知道它是什么,为什么会这样。 (考虑一下&#39; 5,6&#39;对你当前逻辑的影响。)我们不需要初始化arrCommand,我们只检查我们设定的值,所以我&#39我扔了那个。在输入循环之后,您的打印逻辑应该在外面,所以我已经移动了它。 for循环中的控制逻辑不起作用,因此我已切换到更简单的while循环。外部do循环是一个无限循环,所以我已将其删除:

#include <stdio.h>
#include <assert.h>

int main() {
    int i = 0, command = -1;
    int arrCommand[500];

    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

    while (command != 9) {

        int count = scanf("%d", &command);
        assert(count == 1);

        arrCommand[i++] = command;

        if (command == 5) {
            count = scanf(",%d", &arrCommand[i++]);
            assert(count == 1);
        }
    }

    for (int j = 0; j < i; j++)
    {
        if (arrCommand[j] == 5) {
            printf("%d,%d ", arrCommand[j], arrCommand[j + 1]);
            j++;
        } else {
            printf("%d ", arrCommand[j]);
        }
    }

    printf("\n");

    return 0;
}

<强> USAGE

> ./a.out
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
1
3
6
5,100
4 
9
1 3 6 5,100 4 9 
>