«

猜价格

点亮灯 发布于 阅读:136 C++


猜价格.rar

//利用do-while循环编程实现已知商品猜价格. 
#include<iostream> 
#include<ctime>
#include<cstdlib> 
using namespace std;
int main(){
float a,b;
srand(time(0));         //调用随机函数 
b=rand()%(100-1+1)+1;   //生成随机值 

    cout<<"\t\t猜价格(100以内的整数)"<<endl; 
    cout<<b<<endl; //显示系统随机价格 
do  {
    cout<<"请输入:"; 
    cin>>a;
    if (a<b)
        {
            cout<<"猜小了,请继续猜\n"<<endl; 
        }
    if (a>b)
        {
            cout<<"猜大了,请继续\n"<<endl;
        }
}
while(a!=b);
cout<<"恭喜您,猜对了" ;
}