猜价格游戏
来源:百度文库 编辑:神马文学网 时间:2024/06/30 19:01:47
1、猜价格游戏
编写C++程序完成以下功能:
(1) 假定有一件商品,程序用随机数指定该商品的价格(1-1000的整数);
(2) 提示用户猜价格,并输入:若用户猜的价格比商品价格高或低,对用户作出相应的提示;
(3) 直到猜对为止,并给出提示。
#include
#include
using namespace std;
int main(void)
{
int price,guess;
cout<<"有一件商品价格为X,介于1与1000之间,请猜想其价格:\n";
srand(time(NULL));
price=rand()%1000+1;
cin>>guess;
while(guess!=price)
{
if(guess>price)
{
cout<<"猜想价格高于实际价格,请从新猜想\n";
cin>>guess;
}
else
{
cout<<"猜想价格低于实际价格,请从新猜想\n";
cin>>guess;
}
}
cout<<"猜想正确";
system("pause");
return 0;
}