问题及代码:
/*
*Copyright (c) 2015,烟台大学计算机学院
*All rights reserved.
*文件名称:text.cpp
*作者:徐健
*完成日期:2015年3月22日
*版本号:v1.0
*
*问题描述:随机产生一个1000之内的数字,要求用户猜测这个整数。
*输入描述:输入一个猜测的数,猜不对,继续输入
*程序输出:输出猜测结果,知道才对为止
*/
#include<ctime>
#include<cstdlib>
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"数的范围是1000以内"<<endl;
srand(time(0));
int x=rand()%1000;
while(1)
{
cout<<"请输入你猜的数:";
cin>>n;
if(n<x)
cout<<"小了!!!"<<endl;
if(n>x)
cout<<"大了!!!"<<endl;
if(n==x)
{
cout<<"恭喜你,答对了!!!"<<endl;
break;
}
}
return 0;
}
运行结果:
知识点总结:
运用了选择结构以及随机函数。
学习心得:
通过所学知识,来制作出有趣的游戏。