这个代码中出现“?”字符的原因是,你在定义字符数组时使用了错误的语法。在C语言中,字符数组应该用单引号而不是双引号来定义,例如`char wanjia[3] = {'玩', '家', '\0'};`表示一个包含两个字符的字符串,最后一个`\0`表示字符串的结束符。
另外,你可能需要修改do-while循环中的for循环部分,因为它目前没有正确的结束条件。以下是修改后的代码示例:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
char ditu[100] = {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0',
'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0',
'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0',
'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
char wanjia[3] = {'玩', '家', '\0'};
char diren[3] = {'敌', '军', '\0'};
srand((unsigned)time(NULL));
int a = rand() % 10;
int b = rand() % 10;
int c = rand() % 10;
int d = rand() % 10;
int n = 0, m = 0, put, z;
do {
for (n = 0; n < 100; n++) {
printf("%c ", ditu[n]);
if ((n + 1) % 10 == 0) {
printf("\n");
}
}
printf("请输入坐标:");
scanf("%d", &put);
if (put >= 0 && put < 100) {
ditu[put] = '1';
}
for (z = 0; z < 100; z++) {
printf("%c ", ditu[z]);
if ((z + 1) % 10 == 0) {
printf("\n");
}
}
printf("是否继续?(1/0)");
scanf("%d", &m);
} while (m == 1);
return 0;
}
这段代码会循环打印一个地图,接收用户输入的坐标并在地图上标记。然后询问用户是否继续操作,如果用户输入1,则继续下一轮循环,否则结束程序。注意,这里只用到了地图的前100个位置,如果需要使用更多位置,请自行修改代码。
以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。 |