c语言猜数字游戏
因为之前学python小甲鱼教做了猜数字的游戏,在学习C的时候突发奇想用也来做一个,然而因为才开始学,代码运行有问题,我也不知道是哪里出了差错(可能是rand没用对?之前也没用过这个)代码如下
#include <stdio.h>
#include <stdlib.h>
int main()
{
int gusse;
int num = rand();
int option = 10;//有十次机会
do
{
printf("please input the num you gusse:");
scanf("%d",&gusse);
if(gusse = num)
{
printf("WOW YOU ARE SO SMART!!!");break;
}
else if(gusse <= num)
{
printf("GUY!You are so SMALL!!!hhh.And you only have %d option!",option);break;
}
else if(gusse >= num)
{
printf("GUY!You are bigger than that num!And you only have %d option!",option);break;
}
option = option - 1;
}
while(option > 0);
printf("GAME OVER!");
return 0;
} if(gusse = num)这里是== 看我注释
#include <stdio.h>
#include <stdlib.h>
int main()
{
int guess;// 猜测的英文是guess
int num = rand();
int option = 10;//有十次机会
do
{
printf("please input the num you guess:");
scanf("%d",&guess);
if(guess == num)// 这里要写==,你可能受到python的影响了,认为编译器会不错,但实际上不会报错
{
printf("WOW YOU ARE SO SMART!!!\n");break;
}
else if(guess < num)// 这里不要写=的情况
{
printf("GUY!You are so SMALL!!!hhh.And you only have %d option!\n",option);// 既然要让用户继续猜,为什么要退出呢?
}
else if(guess > num)// 这里不要写=的情况
{
printf("GUY!You are bigger than that num!And you only have %d option!\n",option);// 既然要让用户继续猜,为什么要退出呢?
}
option--;//这里用--可提升效率
}
while(option > 0);
printf("GAME OVER!\n");
return 0;
} 本帖最后由 jackz007 于 2022-9-19 23:44 编辑
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define count 5
int main(void)
{
int i , guess , secret ;
time_t t ;
srand((unsigned) time(& t)) ;
secret = rand() % 11 ; // 产生 0 ~ 10 的随机整数
for(i = 0 ; i < count ; i ++) {
printf("please input the num your gusse (0 ~ 10) : ") ;
scanf("%d", & guess) ;
if(guess == secret) {
printf("WOW YOU ARE SO SMART!!!\n") ;
break ;
} else {
if(guess > secret) printf("GUY ! You are bigger than that num !\n") ;
else printf("GUY ! You are so SMALL !!!hhh.\n") ;
if(i < count - 1)printf("And you only have %d option!\n\n" , count - i - 1) ;
}
}
printf("GAME OVER !\n") ;
}
编译、运行实况
D:\\C>g++ -o x x.c
D:\\C>x
please input the num you gusse (0 ~ 10) : 7
GUY ! You are bigger than that num !
And you only have 4 option!
please input the num you gusse (0 ~ 10) : 4
GUY ! You are so SMALL !!!hhh.
And you only have 3 option!
please input the num you gusse (0 ~ 10) : 5
GUY ! You are so SMALL !!!hhh.
And you only have 2 option!
please input the num you gusse (0 ~ 10) : 6
WOW YOU ARE SO SMART!!!
GAME OVER !
D:\\C> 不建议混学,除非你要当程序猿
正是我混学,有时把C的习惯带到Python来 但是你if那点的确要写为== zhangjinxuan 发表于 2022-9-20 07:13
不建议混学,除非你要当程序猿
正是我混学,有时把C的习惯带到Python来
本来只想学Python,然而开学后要弄单片机,不得不学c了 哦~
页:
[1]