c语言作业 猜数字游戏,用C语言实现一个简单的猜数字游戏

#include //头文件输出

#include //应该是时间

#include //里面定义的简单函数

#include //布尔类型头文件

#include //返回非零值否则返回零

int main(){ //主函数

srand(time(NULL)) //每次运行随机数字

while(true) //循环

{

int min=1;

int max=100; //初始范围

int count=0; //猜测次数

const int target = rand ()%max + 1; //产生一个随机的目标数字

while (true) //猜测循环,内循环

{

int guess = 0;

printf("please input a number between %d and %d\n",min,max); //输出这段话

fflush(stdin); //清空缓冲区

scanf("%d",&guess); //获取猜测的数字

++count; //猜测数字加一

if(guessmax) //超出范围,判断

{

printf("the input is put of %d - %d\n",min ,max); //输出这段话

continue; //不执行循环剩下部分,直接进行下一次循环

}

else //否则

{

if (target == guess) //猜中

{

printf("YOU WIN! \nyou have guessed %d times in total.\n",count); //输出这段话

break; //跳出循环

}

else if (target > guess)  //目标比猜得数字大

{

min = guess;

printf("the target is larger than %d\n",guess); //输出这段话

}

else //目标比猜得数字小

{

max = guess;

printf("the target is less than %d\n",guess);

}

}

}

//本轮游戏结束

printf ("Do you want to play again?(Y - yes, N - no)\n"); //输出这段话

fflush (stdin); //请空缓冲区

char c = 'Y'; //定义一个字符

scanf ("%c",&c); //输入一个字符

if ( toupper (c) != 'Y') //判断是否不等于Y

break; //跳出循环

}

return 0; //返回值为零

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值