这是一个类似坦克大战游戏的未完成代码,为什么在运行时会出现“?”字符
#include<time.h>#include<stdlib.h>
#include<stdio.h>
#include<string.h>/*有时会有开局杀是不是很惊喜!*/
int main(){
char ditu={"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[]={"玩家"};
char diren[]={"敌军"};
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<9;n++){
for(m=0;m<9;m++){
printf("%4s ",ditu);
}
printf("\n");
}
printf("\n");
strcpy(ditu,diren);
strcpy(ditu,wanjia);
z = rand()%8;//z=0向左z=1向右z=2向下z=3向上z=4子弹左z=5子弹右z=6子弹上z=7子弹下
if(z==0){//向左
strcpy(ditu,diren);
strcpy(ditu,"0");
b = b-1;
if(b-1<0){
b = b+1;
strcpy(ditu,diren);
continue;
}
}
if(z==1){//向右
strcpy(ditu,diren);
strcpy(ditu,"0");
b = b+1;
if(b+1>8){
b = b-1;
strcpy(ditu,diren);
continue;
}
}
if(z==2){//向下
strcpy(ditu,diren);
strcpy(ditu,"0");
a = a+1;
if(a+1>8){
a = a-1;
strcpy(ditu,diren);
continue;
}
}
if(z==3){//向上
strcpy(ditu,diren);
strcpy(ditu,"0");
a = a-1;
if(a-1<0){
a = a+1;
strcpy(ditu,diren);
continue;
}
}
if(z==4){//发射子弹 向左
if(b!=0){
for(n=0;n<b;n++){
strcpy(ditu,"0");
}
}
else{
continue;
}
}
if(z==5){//发射子弹 向右
if(b!=8){
for(n=8;n>b;n--){
strcpy(ditu,"0");
}
}
else{
continue;
}
}
if(z==6){//发射子弹 向上
if(a!=0){
for(n=0;n<z;n++){
strcpy(ditu,"0");
}
}
else{
continue;
}
}
if(z==7){//发射子弹 向下
if(a!=8){
for(n=8;n>z;n--){
strcpy(ditu,"0");
}
}
else{
continue;
}
}
printf("\n%d\n",z);
//以下是玩家的操作 ,如果没有敌军,就重复一遍srand
for(n=0;n<9;n++){
for(m=0;m<9;m++){
printf("%4s ",ditu);
}
printf("\n");
}
printf("\n");
scanf("%d",&put);
}while(put!=-1);
return 0;
} 这个代码中出现“?”字符的原因是,你在定义字符数组时使用了错误的语法。在C语言中,字符数组应该用单引号而不是双引号来定义,例如`char wanjia = {'玩', '家', '\0'};`表示一个包含两个字符的字符串,最后一个`\0`表示字符串的结束符。
另外,你可能需要修改do-while循环中的for循环部分,因为它目前没有正确的结束条件。以下是修改后的代码示例:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
char ditu = {'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 = {'玩', '家', '\0'};
char diren = {'敌', '军', '\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);
if ((n + 1) % 10 == 0) {
printf("\n");
}
}
printf("请输入坐标:");
scanf("%d", &put);
if (put >= 0 && put < 100) {
ditu = '1';
}
for (z = 0; z < 100; z++) {
printf("%c ", ditu);
if ((z + 1) % 10 == 0) {
printf("\n");
}
}
printf("是否继续?(1/0)");
scanf("%d", &m);
} while (m == 1);
return 0;
}
这段代码会循环打印一个地图,接收用户输入的坐标并在地图上标记。然后询问用户是否继续操作,如果用户输入1,则继续下一轮循环,否则结束程序。注意,这里只用到了地图的前100个位置,如果需要使用更多位置,请自行修改代码。
以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
页:
[1]