鱼C论坛

 找回密码
 立即注册
查看: 322|回复: 1

这是个类似坦克大战的代码,但在输出时会莫名其妙地出现“?”,怎么解决

[复制链接]
发表于 2023-12-1 20:27:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<time.h>
#include<stdlib.h>
#include<stdio.h>//子弹那里错了,记得改 11.30
#include<string.h>/*有时会有开局杀是不是很惊喜!*/
int main(){
        char ditu[9][9][3]={"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[5]="玩家";
        char diren[5]="敌军";
        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,z;
        char put[5];
        printf("欢迎开始坦克大战!\n有时会有开局杀,好刺激的!\n本游戏采用wsad j模式\n另外,本游戏敌军优先级高于玩家哦。\n输入-1即可结束游戏,或者你被干翻");
        do{
                for(n=0;n<9;n++){
                        for(m=0;m<9;m++){
                                printf("%-4s",ditu[n][m]);
                        }
                        printf("\n");
                }
                    printf("\n");
                        strcpy(ditu[a][b],diren);
                        strcpy(ditu[c][d],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[a][b-1],diren);
                        strcpy(ditu[a][b],"0");
                        b = b-1;
                    if(b-1<0){
                        b = b+1;
                        strcpy(ditu[a][b],diren);
                        continue;
                }
        }
                if(z==1){//向右
                        strcpy(ditu[a][b+1],diren);
                        strcpy(ditu[a][b],"0");
                        b = b+1;
                    if(b+1>8){
                        b = b-1;
                        strcpy(ditu[a][b],diren);
                        continue;
                }
        }
                if(z==2){//向下
                        strcpy(ditu[a+1][b],diren);
                        strcpy(ditu[a][b],"0");
                        a = a+1;
                    if(a+1>8){
                        a = a-1;
                        strcpy(ditu[a][b],diren);
                        continue;
                }
        }
                if(z==3){//向上
                        strcpy(ditu[a-1][b],diren);
                        strcpy(ditu[a][b],"0");
                        a = a-1;
                    if(a-1<0){
                        a = a+1;
                        strcpy(ditu[a][b],diren);
                        continue;
                }
        }
                if(z==4){//发射子弹 向左
                        if(b!=0){
                        for(n=0;n<b;n++){
                                strcpy(ditu[z][n],"0");
                    }
                }
                    else{
                            continue;
                        }
        }
                if(z==5){//发射子弹 向右
                        if(b!=8){
                        for(n=8;n>b;n--){
                                strcpy(ditu[z][n],"0");
                        }
                }
                    else{
                            continue;
                    }
        }
                if(z==6){//发射子弹 向上
                        if(a!=0){
                        for(n=0;n<z;n++){
                                strcpy(ditu[n][b],"0");
                        }
                }
                    else{
                            continue;
                    }
        }
                if(z==7){//发射子弹 向下
                        if(a!=8){
                        for(n=8;n>z;n--){
                                strcpy(ditu[n][b],"0");
                        }
                }
                    else{
                            continue;
                        }
        }
        printf("\n%d\n",z);
                //以下是玩家的操作 ,如果没有敌军,就重复一遍srand
                //wsad
                scanf("%s",&put);
                if(strcmp(put,"w")==0){//w向上
                        strcpy(ditu[a-1][b],wanjia);
                        strcpy(ditu[a][b],"0");
                        a = a-1;
                    if(a-1<0){
                        a = a+1;
                        strcpy(ditu[a][b],wanjia);
                        continue;
                }
        }
                if(strcmp(put,"s")==0){//s向下
                        strcpy(ditu[a+1][b],wanjia);
                        strcpy(ditu[a][b],"0");
                        a = a+1;
                    if(a+1>8){
                        a = a-1;
                        strcpy(ditu[a][b],wanjia);
                        continue;
                }
        }
                if(strcmp(put,"a")==0){//s向左
                        strcpy(ditu[a][b-1],wanjia);
                        strcpy(ditu[a][b],"0");
                        b = b-1;
                    if(b-1<0){
                        b = b+1;
                        strcpy(ditu[a][b],wanjia);
                        continue;
                }
        }
                if(strcmp(put,"d")==0){//d向右
                        strcpy(ditu[a][b+1],wanjia);
                        strcpy(ditu[a][b],"0");
                        b = b+1;
                    if(b+1>8){
                        b = b-1;
                        strcpy(ditu[a][b],wanjia);
                        continue;
                }
        }       
               
               
               
                for(n=0;n<9;n++){
                        for(m=0;m<9;m++){
                                printf("%-4s",ditu[n][m]);
                        }
                        printf("\n");
                }
                printf("\n");
        }while(put!="-1");
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-12-1 20:28:03 | 显示全部楼层
代码中的问题可能是由于字符编码不一致导致的。在C语言中,字符是以ASCII码形式存储的。但是,部分操作系统和文本编辑器默认使用的字符编码可能不是ASCII码。因此,在输出时会出现乱码或者“?”。

要解决这个问题,可以尝试以下几种方法:
1. 确保代码文件的编码与操作系统的默认编码一致。可以在文本编辑器中修改文件的编码类型为UTF-8或者ASCII编码。
2. 在输出字符串之前,将字符数组转换为宽字符数组,并使用wprintf函数来输出。示例代码如下:
  1. #include <wchar.h>
  2. #include <locale.h>
  3. int main() {
  4.     setlocale(LC_ALL, "");  // 设置本地化环境为当前用户的环境
  5.     char ditu[100] = "坦克大战";  // 定义一个宽字符数组
  6.     wchar_t wditu[100];
  7.     mbstowcs(wditu, ditu, sizeof(ditu));  // 将字符数组转换为宽字符数组
  8.     wprintf(L"%ls\n", wditu);  // 使用wprintf函数输出宽字符数组
  9.     return 0;
  10. }
复制代码

请注意,在使用宽字符数组和wprintf函数时,需要包含<wchar.h>头文件,并且使用setlocale函数将本地化环境设置为当前用户的环境。

希望以上方法能够解决你遇到的问题。如果还有其他疑问,请随时追问。

以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-1 01:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表