您的位置:首页 > 编程语言 > C语言/C++

c语言编程小实例之一-------猜数

2014-03-12 17:03 127 查看
题目:猜一个十以内的整数并返回答案。

c语言编程:

#include<stdlib.h>//stdlib.h,这个是最常用的系统函数库,提供rand()、srand()、exit()等函数
#include<stdio.h>//stdio.h,这个就是程序的输入输出函数库,它提供int getchar()和int putchar()等函数
#include <time.h>//使用当前时钟做种子
int main()
{
int a, b;
srand( (unsigned)time( NULL ) );//初始化随机数 !!!
a=rand()%10;//随机产生数的编程
printf("%d:\n",a);
printf("请输入您猜到的数字(0~10):\n");
scanf("%d",&b);
//scanf("%d,%d", &a, &b);
//如果a和b都被成功读入,那么scanf的返回值就是2
//如果只有a被成功读入,返回值为1
//如果a和b都未被成功读入,返回值为0
printf("%d:\n",b);
if(a==b)
printf("恭喜您!\n");
else if(a<b)
printf("对不起,您猜大了\n");
else
printf("对不起,您猜小了\n");//a不能随便用rand()替代,那样会产生多个随机数
return 0;//不能漏掉哦
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: