这是个类似坦克大战的代码,但在输出时会莫名其妙地出现“?”,怎么解决
#include<time.h>#include<stdlib.h>
#include<stdio.h>//子弹那里错了,记得改 11.30
#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,z;
char put;
printf("欢迎开始坦克大战!\n有时会有开局杀,好刺激的!\n本游戏采用wsad j模式\n另外,本游戏敌军优先级高于玩家哦。\n输入-1即可结束游戏,或者你被干翻");
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
//wsad
scanf("%s",&put);
if(strcmp(put,"w")==0){//w向上
strcpy(ditu,wanjia);
strcpy(ditu,"0");
a = a-1;
if(a-1<0){
a = a+1;
strcpy(ditu,wanjia);
continue;
}
}
if(strcmp(put,"s")==0){//s向下
strcpy(ditu,wanjia);
strcpy(ditu,"0");
a = a+1;
if(a+1>8){
a = a-1;
strcpy(ditu,wanjia);
continue;
}
}
if(strcmp(put,"a")==0){//s向左
strcpy(ditu,wanjia);
strcpy(ditu,"0");
b = b-1;
if(b-1<0){
b = b+1;
strcpy(ditu,wanjia);
continue;
}
}
if(strcmp(put,"d")==0){//d向右
strcpy(ditu,wanjia);
strcpy(ditu,"0");
b = b+1;
if(b+1>8){
b = b-1;
strcpy(ditu,wanjia);
continue;
}
}
for(n=0;n<9;n++){
for(m=0;m<9;m++){
printf("%-4s",ditu);
}
printf("\n");
}
printf("\n");
}while(put!="-1");
return 0;
} 代码中的问题可能是由于字符编码不一致导致的。在C语言中,字符是以ASCII码形式存储的。但是,部分操作系统和文本编辑器默认使用的字符编码可能不是ASCII码。因此,在输出时会出现乱码或者“?”。
要解决这个问题,可以尝试以下几种方法:
1. 确保代码文件的编码与操作系统的默认编码一致。可以在文本编辑器中修改文件的编码类型为UTF-8或者ASCII编码。
2. 在输出字符串之前,将字符数组转换为宽字符数组,并使用wprintf函数来输出。示例代码如下:
#include <wchar.h>
#include <locale.h>
int main() {
setlocale(LC_ALL, "");// 设置本地化环境为当前用户的环境
char ditu = "坦克大战";// 定义一个宽字符数组
wchar_t wditu;
mbstowcs(wditu, ditu, sizeof(ditu));// 将字符数组转换为宽字符数组
wprintf(L"%ls\n", wditu);// 使用wprintf函数输出宽字符数组
return 0;
}
请注意,在使用宽字符数组和wprintf函数时,需要包含<wchar.h>头文件,并且使用setlocale函数将本地化环境设置为当前用户的环境。
希望以上方法能够解决你遇到的问题。如果还有其他疑问,请随时追问。
以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
页:
[1]