#include "stdioh"//包含输入输出头文件,printf就在其中
main()
{
int x1=0xabc,x2=0xdef;//声明x1、x2为有符号整型变量并用十六进制赋初值
x2-=x1;//用x2的值减去x1的值并将结果赋给x2
printf("%x\n",x2);//用十六进制输出x2的值
}
结果是十六进制333,即十进制的819。
我运行了,解释是GO west,youn 最后没有‘g’
因为你用了while循环
输入之后先是读出ch = 'G'
判断ch是否为'g',
不是所以打印G
接着读下一个字符 得ch=‘O’
判断ch是否为'g',
不是所以打印O
……
到读到g时,ch=‘g’
判断ch是否为'g',
是,所以循环结束g不打印
循环结束,程序结束。
打印过程中为换行或加其他符号,所以是
GO west,youn
你在程序中打印点其他的,或换行就可以很清楚看出来怎么回事了。
比如:
# include <stdioh>
int main(void)
{
char ch;
scanf(" %c",&ch);
while (ch != 'g')
{
printf("%c",ch);
printf("\n"); //换行
scanf(" %c",&ch);
}
return 0;
}
"扫雷"小游戏C代码
#include<stdioh>
#include<mathh>
#include<timeh>
#include<stdlibh>
main( )
{char a[102][102],b[102][102],c[102][102],w;
int i,j; /循环变量/
int x,y,z[999]; /雷的位置/
int t,s; /标记/
int m,n,lei; /计数/
int u,v; /输入/
int hang,lie,ge,mo; /自定义变量/
srand((int)time(NULL)); /启动随机数发生器/
leb1: /选择模式/
printf("\n 请选择模式:\n 1标准 2自定义\n");
scanf("%d",&mo);
if(mo==2) /若选择自定义模式,要输入三个参数/
{do
{t=0; printf("请输入\n行数 列数 雷的个数\n");
scanf("%d%d%d",&hang,&lie,&ge);
if(hang<2){printf("行数太少\n"); t=1;}
if(hang>100){printf("行数太多\n");t=1;}
if(lie<2){printf("列数太少\n");t=1;}
if(lie>100){printf("列数太多\n");t=1;}
if(ge<1){printf("至少要有一个雷\n");t=1;}
if(ge>=(hanglie)){printf("雷太多了\n");t=1;}
}while(t==1);
}
else{hang=10,lie=10,ge=10;} /否则就是选择了标准模式(默认参数)/
for(i=1;i<=ge;i=i+1) /确定雷的位置/
{do
{t=0; z[i]=rand( )%(hanglie);
for(j=1;j<i;j=j+1){if(z[i]==z[j]) t=1;}
}while(t==1);
}
for(i=0;i<=hang+1;i=i+1) /初始化a,b,c/
{for(j=0;j<=lie+1;j=j+1) {a[i][j]='1'; b[i][j]='1'; c[i][j]='0';} }
for(i=1;i<=hang;i=i+1)
{for(j=1;j<=lie;j=j+1) {a[i][j]='+';} }
for(i=1;i<=ge;i=i+1) /把雷放入c/
{x=z[i]/lie+1; y=z[i]%lie+1; c[x][y]='#';}
for(i=1;i<=hang;i=i+1) /计算b中数字/
{for(j=1;j<=lie;j=j+1)
{m=48;
if(c[i-1][j-1]=='#')m=m+1; if(c[i][j-1]=='#')m=m+1;
if(c[i-1][j]=='#')m=m+1; if(c[i+1][j+1]=='#')m=m+1;
if(c[i][j+1]=='#')m=m+1; if(c[i+1][j]=='#')m=m+1;
if(c[i+1][j-1]=='#')m=m+1; if(c[i-1][j+1]=='#')m=m+1;
b[i][j]=m;
}
}
for(i=1;i<=ge;i=i+1) /把雷放入b中/
{x=z[i]/lie+1; y=z[i]%lie+1; b[x][y]='#';}
lei=ge; /以下是游戏设计/
do
{leb2: /输出/
system("cls");printf("\n\n\n\n");
printf(" ");
for(i=1;i<=lie;i=i+1)
{w=(i-1)/10+48; printf("%c",w);
w=(i-1)%10+48; printf("%c ",w);
}
printf("\n |");
for(i=1;i<=lie;i=i+1){printf("---|");}
printf("\n");
for(i=1;i<=hang;i=i+1)
{w=(i-1)/10+48; printf("%c",w);
w=(i-1)%10+48; printf("%c |",w);
for(j=1;j<=lie;j=j+1)
{if(a[i][j]=='0')printf(" |");
else printf(" %c |",a[i][j]);
}
if(i==2)printf(" 剩余雷个数");
if(i==3)printf(" %d",lei);
printf("\n |");
for(j=1;j<=lie;j=j+1){printf("---|");}
printf("\n");
}
scanf("%d%c%d",&u,&w,&v); /输入/
u=u+1,v=v+1;
if(w!='#'&&a[u][v]=='@')
goto leb2;
if(w=='#')
{if(a[u][v]=='+'){a[u][v]='@'; lei=lei-1;}
else if(a[u][v]=='@'){a[u][v]=''; lei=lei+1;}
else if(a[u][v]==''){a[u][v]='+';}
goto leb2;
}
a[u][v]=b[u][v];
leb3: /打开0区/
t=0;
if(a[u][v]=='0')
{for(i=1;i<=hang;i=i+1)
{for(j=1;j<=lie;j=j+1)
{s=0;
if(a[i-1][j-1]=='0')s=1; if(a[i-1][j+1]=='0')s=1;
if(a[i-1][j]=='0')s=1; if(a[i+1][j-1]=='0')s=1;
if(a[i+1][j+1]=='0')s=1; if(a[i+1][j]=='0')s=1;
if(a[i][j-1]=='0')s=1; if(a[i][j+1]=='0')s=1;
if(s==1)a[i][j]=b[i][j];
}
}
for(i=1;i<=hang;i=i+1)
{for(j=lie;j>=1;j=j-1)
{s=0;
if(a[i-1][j-1]=='0')s=1; if(a[i-1][j+1]=='0')s=1;
if(a[i-1][j]=='0')s=1; if(a[i+1][j-1]=='0')s=1;
if(a[i+1][j+1]=='0')s=1; if(a[i+1][j]=='0')s=1;
if(a[i][j-1]=='0')s=1; if(a[i][j+1]=='0')s=1;
if(s==1)a[i][j]=b[i][j];
}
}
for(i=hang;i>=1;i=i-1)
{for(j=1;j<=lie;j=j+1)
{s=0;
if(a[i-1][j-1]=='0')s=1; if(a[i-1][j+1]=='0')s=1;
if(a[i-1][j]=='0')s=1; if(a[i+1][j-1]=='0')s=1;
if(a[i+1][j+1]=='0')s=1; if(a[i+1][j]=='0')s=1;
if(a[i][j-1]=='0')s=1; if(a[i][j+1]=='0')s=1;
if(s==1)a[i][j]=b[i][j];
}
}
for(i=hang;i>=1;i=i-1)
{for(j=lie;j>=1;j=j-1)
{s=0;
if(a[i-1][j-1]=='0')s=1; if(a[i-1][j+1]=='0')s=1;
if(a[i-1][j]=='0')s=1; if(a[i+1][j-1]=='0')s=1;
if(a[i+1][j+1]=='0')s=1;if(a[i+1][j]=='0')s=1;
if(a[i][j-1]=='0')s=1; if(a[i][j+1]=='0')s=1;
if(s==1)a[i][j]=b[i][j];
}
}
for(i=1;i<=hang;i=i+1) /检测0区/
{for(j=1;j<=lie;j=j+1)
{if(a[i][j]=='0')
{if(a[i-1][j-1]=='+'||a[i-1][j-1]=='@'||a[i-1][j-1]=='')t=1;
if(a[i-1][j+1]=='+'||a[i-1][j+1]=='@'||a[i-1][j+1]=='')t=1;
if(a[i+1][j-1]=='+'||a[i+1][j-1]=='@'||a[i+1][j-1]=='')t=1;
if(a[i+1][j+1]=='+'||a[i+1][j+1]=='@'||a[i+1][j+1]=='')t=1;
if(a[i+1][j]=='+'||a[i+1][j]=='@'||a[i+1][j]=='')t=1;
if(a[i][j+1]=='+'||a[i][j+1]=='@'||a[i][j+1]=='')t=1;
if(a[i][j-1]=='+'||a[i][j-1]=='@'||a[i][j-1]=='')t=1;
if(a[i-1][j]=='+'||a[i-1][j]=='@'||a[i-1][j]=='')t=1;
}
}
}
if(t==1)goto leb3;
}
n=0; /检查结束/
for(i=1;i<=hang;i=i+1)
{for(j=1;j<=lie;j=j+1)
{if(a[i][j]!='+'&&a[i][j]!='@'&&a[i][j]!='')n=n+1;}
}
}
while(a[u][v]!='#'&&n!=(hanglie-ge));
for(i=1;i<=ge;i=i+1) /游戏结束/
{x=z[i]/lie+1; y=z[i]%lie+1; a[x][y]='#'; }
printf(" ");
for(i=1;i<=lie;i=i+1)
{w=(i-1)/10+48; printf("%c",w);
w=(i-1)%10+48; printf("%c ",w);
}
printf("\n |");
for(i=1;i<=lie;i=i+1){printf("---|");}
printf("\n");
for(i=1;i<=hang;i=i+1)
{w=(i-1)/10+48; printf("%c",w);
w=(i-1)%10+48; printf("%c |",w);
for(j=1;j<=lie;j=j+1)
{if(a[i][j]=='0')printf(" |");
else printf(" %c |",a[i][j]);
}
if(i==2)printf(" 剩余雷个数");
if(i==3)printf(" %d",lei); printf("\n |");
for(j=1;j<=lie;j=j+1) {printf("---|");}
printf("\n");
}
if(n==(hanglie-ge)) printf("你成功了!\n");
else printf(" 游戏结束!\n");
printf(" 重玩请输入1\n");
t=0;
scanf("%d",&t);
if(t==1)goto leb1;
}
/注:在DEV c++上运行通过。行号和列号都从0开始,比如要确定第0行第9列不是“雷”,就在0和9中间加入一个字母,可以输入0a9三个字符再按回车键。3行7列不是雷,则输入3a7回车;第8行第5列是雷,就输入8#5回车,9行0列是雷则输入9#0并回车/
main() '主函数开始
{int i; '定义整形变量 i
for(i=0;i<3;i++) 'for循环开始,i的值从0到2 依次循环
switch(i) '判断i的值
case 0: prinft("%d",i) 如果i的值是0 那么打印i ,%d 表示打印整形变量
case 2: prinft("%d",i) 如果i的值是2 那么打印i ,%d 表示打印整形变量
default: prinft("%d",i) 如果不满足上面的条件(就是说i=1) 打印i ,%d
表示打印整形变量
} 结束
多了个 {
}
你说的那个 因为没有break语句 i=0的时候,下面的打印语句也执行了,所以有3个0 i=1的时候只执行最后一个语句 所以有1个1 i=2的时候执行了最后面2个语句,所以有2个2,记得可以用break语句防止这种情况
把 scanf("%c",&b);
改成 scanf(" %c",&b); // %c前加一个空格, 表述过滤掉空白字符, 因为前面的s后面有个回车呢
你也可以直接在第一次的位置输入
sb
1max=-32768;
min=32767;
如果你编译平台是16位的 如TC 那么int类型变量分配16位二进制数 第一位为符号位 有15位表示数值 则其取值范围-2的15次方到2的15次方减1
即-2^15~2^15-1 得-32768~32767 所以 int最小数就是 -32768最大数就是
32767
如果你编译平台是32位的 int 范围是-2^31~2^31-1
2sum +=integer; 就是sum=sum+integer c语言提供这样sum +=integer语法 死规绝
3程序定义max=-32768; min=32767;最大数max先赋最小值 最小数min先赋最大值
如果你不赋初始值 max和min随即取数 判断将不准确
比较是经过if(integer>max)max=integer;if(integer<min)min=integer;这两条语句实现 系统不会默认自动比较
下落的小鸟
#include<stdioh>
#include<stdlibh>
#include<conioh>
#include<timeh>
#include<Windowsh>
int Grade = 1, Score = 0, Max_blank = 9, Distance = 18;
struct Birds{int x; int y;}; //定义一种Birds数据类型(含3个成员)
Birds Bird = (Birds)malloc(sizeof(Birds)); //定义Birds类型 指针变量Bird并赋初值
struct Bg{int x, y; int l_blank; Bg pri; Bg next;}; //定义一种Bg数据类型(含5个成员)
Bg Bg1 = (Bg)malloc(sizeof(Bg)); //定义Bg类型 指针变量Bg1并赋初值
void Position(int x, int y) //光标定位函数(用于指定位置输出)
{COORD pos = { x - 1, y - 1 };
HANDLE Out = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(Out, pos);
}
void Csh( ) //初始化界面
{
printf("══════════════════════════════════════\n");
printf(" ■■ ■■ C语言版 Flappy Bird \n");
printf(" ■■ ■■\n");
printf(" ■■ ■■\n");
printf(" ■■ ■■ 瞎搞人:yyposs原创\n");
printf(" ■■ ■■ 瞎搞日期:20142\n");
printf(" ■■ ■■\n");
printf(" ■■ ■■ 改编:鸣蝉百20217\n");
printf(" ■■ ■■ *** 作:按向上方向键让小鸟起飞\n");
printf(" ■■\n");
printf(" ■■\n");
printf(" ■■ ■■\n");
printf(" ■■ ■■\n");
printf(" ■■ ■■\n");
printf(" ■■ ■■\n");
printf(" ■■ ■■ DEVc++运行通过\n");
printf("══════════════════════════════════════\n");
printf(" 按键继续…");
getch( );
system("cls");
}
void PrFK( ) //输出方框(游戏范围区)
{int i;
Position(1, 1); printf("╔"); Position(79, 1); printf("╗");
Position(1, 24); printf("╚"); Position(79, 24); printf("╝");
for (i = 3; i <= 78; i += 2){Position(i, 1); printf("═"); Position(i, 24); printf("═");}
for(i=2;i<=23;i++)
{ Position(1,i); printf("║");if(i<11)printf("0%d",i-1);else printf("%d",i-1);
Position(79,i); printf("║");
}
Position(4, 25); printf("小鸟即将出现,请准备按键起飞… ");
getch( );
Position(4, 25); printf(" ");
}
void CreatBg( ) //创建障碍物坐标(便于打印输出)
{Bg Bg2 = (Bg)malloc(sizeof(Bg));
Bg1->x = 90; Bg1->y = 8; //确定障碍物的一对基本坐标(此时值是在游戏框之外)
Bg2->x = Bg1->x + Distance; Bg2->y = 9; //下一障碍物的基本坐标x、y
Bg1->l_blank = Max_blank - Grade; //障碍物上下两部分之间的空白距离l_blank
Bg2->l_blank = Max_blank - Grade;
Bg1->next = Bg2; Bg1->pri = Bg2;
Bg2->next = Bg1; Bg2->pri = Bg1;
}
void InsertBg(Bg p) //随机改变障碍物的y坐标,让空白通道有上下变化
{int temp;
Bg Bgs = (Bg)malloc(sizeof(Bg));
Bgs->x = p->pri->x + Distance;
Bgs->l_blank = Max_blank - Grade;
srand((int)time(0)); //启动随机数发生器
temp = rand( ); //产生一个随机数并赋值给temp
if (temp % 2 == 0)
{if ((temp % 4 + p->pri->y + Max_blank - Grade)<21)
Bgs->y = p->pri->y + temp % 4;
else Bgs->y = p->pri->y;
}
else
{if ((p->pri->y - temp % 4)>2)Bgs->y = p->pri->y - temp % 4;
else Bgs->y = p->pri->y;
}
Bgs->pri = p->pri; Bgs->next = p;
p->pri->next = Bgs; p->pri = Bgs;
}
void CreatBird( ) //建立小鸟的坐标(初始打印输出小鸟的位置)
{Bird->x = 41; Bird->y = 10;}
int CheckYN(Bg q) //判断游戏结束与否(值为0是要结束,为1没有要结束)
{Bg p = q; int i = 0;
while (++i <= 5)
{if (Bird->y>23)return 0;
if (Bird->x == p->x&&Bird->y <= p->y)return 0;
if ((Bird->x == p->x || Bird->x == p->x + 1 || Bird->x == p->x + 2) && Bird->y == p->y)return 0;
if (Bird->x == p->x&&Bird->y>p->y + p->l_blank)return 0;
if ((Bird->x == p->x || Bird->x == p->x + 1 || Bird->x == p->x + 2) && Bird->y == p->y + p->l_blank)
return 0;
p = p->next;
}
return 1;
}
void Check_Bg(Bg q) //核查开头的障碍物坐标是否在游戏区内
{Bg p = q; int i = 0, temp;
while (++i <= 5)
{if (p->x>-4)p = p->next;
else
{srand((int)time(0)); temp = rand();
if (temp % 2 == 0)
{if ((temp % 4 + p->y + Max_blank - Grade)<21)p->y = p->y + temp % 4;
else p->y = p->y; p->x = p->pri->x + Distance;
p->l_blank = Max_blank - Grade;
}
else
{if ((p->y - temp % 4)>2)p->y = p->y - temp % 4;
else p->y = p->y; p->x = p->pri->x + Distance;
p->l_blank = Max_blank - Grade;
}
}
}
}
void Prt_Bg(Bg q) //打印输出障碍物(依据其x、y坐标进行相应输出)
{Bg p = q; int i = 0, k, j;
while (++i <= 5)
{if (p->x>0 && p->x <= 78)
{for (k = 2; k<p->y; k++){Position(p->x + 1, k); printf("■"); printf("■"); printf(" ");}
Position(p->x, p->y);
printf("■"); printf("■"); printf("■"); printf(" ");
Position(p->x, p->y + p->l_blank);
printf("■"); printf("■"); printf("■"); printf(" ");
k = k + p->l_blank + 1;
for (k; k <= 23; k++){Position(p->x + 1, k); printf("■"); printf("■"); printf(" ");}
}
p = p->next;
if (p->x == 0)
{for (j = 2; j<p->y; j++){Position(p->x + 1, j); printf(" "); printf(" ");}
Position(p->x + 1, p->y);
printf(" "); printf(" "); printf(" ");
Position(p->x + 1, p->y + Max_blank - Grade);
printf(" "); printf(" "); printf(" ");
j = j + Max_blank - Grade + 1;
for (j; j <= 22; j++){Position(p->x + 1, j); printf(" "); printf(" ");}
}
}
}
void PrtBird( ) //打印输出小鸟
{Position(Bird->x, Bird->y - 1); printf(" ");
Position(Bird->x, Bird->y); printf("Ю");
Position(38, 2); printf("Score:%d", Score);
}
void Loop_Bg(Bg q) //障碍物x坐标左移,并记录成绩
{Bg p = q; int i = 0;
while (++i <= 5)
{p->x = p->x - 1; p = p->next;
if (Bird->x == p->x)
{Score += 1;
if (Score % 4 == 0 && Grade<4)Grade++;
}
}
}
int main( )
{int i = 0; int t;
while (1)
{
Csh( );PrFK( );CreatBg( );
InsertBg(Bg1);InsertBg(Bg1);InsertBg(Bg1);
CreatBird( );
while (1)
{if (!CheckYN(Bg1))break;
Check_Bg(Bg1);Prt_Bg(Bg1);
PrtBird( );Loop_Bg(Bg1);
Bird->y = Bird->y + 1;
if (GetAsyncKeyState(VK_UP)) //按下了向上方向键
{Position(Bird->x, Bird->y - 1);printf(" ");
Bird->y = Bird->y - 4;
}
Sleep(200); //程序延时200毫秒(数值大小决定游戏速度快慢)
i = 0;
}
Position(6, 25);
printf("游戏结束! 请输入:0退出 1重玩");
scanf("%d",&t);
if (t==0)break;
system("cls"); Score = 0;
}
return 0;
}
以上就是关于c语言一小段程序求解全部的内容,包括:c语言一小段程序求解、C语言小程序不懂!、一种C语言小游戏程序设计(程序已经附上)等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)