C 在'之前应为标识符;(';代币

C 在'之前应为标识符;(';代币,c,C,请帮帮我。我找不到有什么问题。其中一个括号在if语句中有问题,但我找不到 26错误:在“(”标记之前应该有标识符 #包括 #包括 #包括 #定义第20行 #定义COLS 20 int main(); int i,j,方向,x,y; 炭板[行][COLS]; srand((无符号)时间(NULL)); 对于(i=0;i

请帮帮我。我找不到有什么问题。其中一个括号在if语句中有问题,但我找不到

26错误:在“(”标记之前应该有标识符

#包括
#包括
#包括
#定义第20行
#定义COLS 20
int main();
int i,j,方向,x,y;
炭板[行][COLS];
srand((无符号)时间(NULL));
对于(i=0;i

我想创建一个大小为20*20的数组,并在屏幕上用圆点打印。然后我将随机选择一个坐标,并在rand定义的坐标中打印一个“*”图标。我想在数组中移动*。我试图编写它,但不知道它是否有效。

您似乎缺少很多“{”和“}”(特别是在主循环中?)。如果后面只有一行,您不必总是将它们放在循环和语句中,但这是一种良好的编码实践,它可以帮助您更快地找到问题。我建议您这样做,如果仍然不起作用,请编辑您的答案

if (board[x][y+1] != '.') && (board[x+1][y] != '.') && (board[x-1][y] != '.') && (board[x][y-1] != '.')
 && (board[x+1][y+1] != '.') && (board[x-1][y+1] != '.') && (board[x+1][y-1] != '.') && (board[x-1][y-1] != '.')
应该是

if ((board[x][y+1] != '.') && (board[x+1][y] != '.') && (board[x-1][y] != '.') && (board[x][y-1] != '.')
 && (board[x+1][y+1] != '.') && (board[x-1][y+1] != '.') && (board[x+1][y-1] != '.') && (board[x-1][y-1] != '.'))
您需要附上所有这些条件


尽管唯一有条件执行的语句是紧靠其下方的
break

编译错误来自:

if (board[x][y+1] != '.') &&
如果
,则
的正确语法为:

if ( condition )
    statement;
如果有多个测试,则需要将它们组合到一个条件中;在这种情况下:

if ( (board[x][y+1] != '.') && (board[x+1][y] != '.') /* && ... */ )
    break;
在这种情况下,我建议在
中断处使用
{}
,即使它只是一个语句,因为它是一个非常大的
if

但是,此测试实际上是不必要的。您测试的条件与在
开关中测试的条件相同,因此没有必要全部测试两次。如果
,您可以完全删除

开关
内,
break;
语句应该在其
if
块之外。无论是否找到匹配的

\include,您都不想陷入下一种情况
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define ROWS 20
#define COLS 20

int main(void)
{
    int i, j, direction, x, y;
    char board[ROWS][COLS];

    srand ((unsigned) time(NULL));

    for(i=0; i < ROWS; i++)
        for(j=0; j < COLS; j++)
                board[i][j] = '.';

    x = rand() % 20;
    y = rand() % 20;
    board[x][y] = '*';

    direction = rand() % 8;


        if ((board[x][y+1] != '.') && (board[x+1][y] != '.') && (board[x-1][y] != '.') && (board[x][y-1] != '.') && (board[x+1][y+1] != '.') && (board[x-1][y+1] != '.') && (board[x+1][y-1] != '.') && (board[x-1][y-1] != '.')) 
            return;

    switch (direction) {
            case 0: if (board[x][y+1] == '.')
                    y++;
                break; 
            case 1: if (board[x+1][y] == '.')
                x++;
                break; 
            case 2: if (board [x-1][y] == '.')
                    x--;
                break; 
            case 3: if (board[x][y-1] == '.')
                    y--;
                break;
            case 4: if (board[x+1][y+1] == '.'){
                    x++;
                    y++;
            }
                break;
            case 5: if (board[x-1][y+1] == '.'){
                    x--;
                    y++;
            }
                break;
            case 6: if (board[x+1][y-1] == '.'){
                    x++;
                    y--;
            }
                break;
            case 7: if (board[x-1][y-1] == '.'){
                    x--;
                    y--;
            }
                break;
        }

        for(i=0; i < ROWS; i++) {
            for(j=0; j < COLS; j++)
                printf("%2d", board[i][j]);

        printf("\n");
    }

    return 0;
}
#包括 #包括 #定义第20行 #定义COLS 20 内部主(空) { int i,j,方向,x,y; 炭板[行][COLS]; srand((无符号)时间(NULL)); 对于(i=0;i

正如上面提到的-您没有将if语句括在两个括号中。另外,一个单独的break语句也没有任何作用。您是说return(exit)?

伙计,我不是调试器。尝试使用valgrind。您可以尝试正确缩进代码。这应该很明显……这是一个非常短的
main()
call,在那里。错误就在你的if语句第26行,正如你所说的。试着把你的if语句括在一个括号里。我倾向于指出,你的编译器在你问问题之前就已经回答了你的问题:它告诉了你到底出了什么问题。你应该听它。而且,开关中的所有
break
似乎都是be放置错误。好的,我编辑了我的程序,现在它可以毫无错误地执行,但它显示的是46而不是点。为什么?@bumi
%2d
打印数字,也许你的意思是
%c”
当然!谢谢您,先生!好的,我一切都好了,但我必须在程序中移动星形图标。现在只是打印图标并完成。我想switch语句没有任何作用。我必须添加系统(cls)。因此,当我执行它时,窗口应该像那样保持打开状态,我应该做什么
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define ROWS 20
#define COLS 20

int main(void)
{
    int i, j, direction, x, y;
    char board[ROWS][COLS];

    srand ((unsigned) time(NULL));

    for(i=0; i < ROWS; i++)
        for(j=0; j < COLS; j++)
                board[i][j] = '.';

    x = rand() % 20;
    y = rand() % 20;
    board[x][y] = '*';

    direction = rand() % 8;


        if ((board[x][y+1] != '.') && (board[x+1][y] != '.') && (board[x-1][y] != '.') && (board[x][y-1] != '.') && (board[x+1][y+1] != '.') && (board[x-1][y+1] != '.') && (board[x+1][y-1] != '.') && (board[x-1][y-1] != '.')) 
            return;

    switch (direction) {
            case 0: if (board[x][y+1] == '.')
                    y++;
                break; 
            case 1: if (board[x+1][y] == '.')
                x++;
                break; 
            case 2: if (board [x-1][y] == '.')
                    x--;
                break; 
            case 3: if (board[x][y-1] == '.')
                    y--;
                break;
            case 4: if (board[x+1][y+1] == '.'){
                    x++;
                    y++;
            }
                break;
            case 5: if (board[x-1][y+1] == '.'){
                    x--;
                    y++;
            }
                break;
            case 6: if (board[x+1][y-1] == '.'){
                    x++;
                    y--;
            }
                break;
            case 7: if (board[x-1][y-1] == '.'){
                    x--;
                    y--;
            }
                break;
        }

        for(i=0; i < ROWS; i++) {
            for(j=0; j < COLS; j++)
                printf("%2d", board[i][j]);

        printf("\n");
    }

    return 0;
}