请教各路大神,一个C++猜数字的问题~~谢谢!

Createa“GuesstheNumber”game!Thecomputershouldgeneratearandomnumberbetween1and100.Ifyo... Create a “Guess the Number” game!

The computer should generate a random number between 1 and 100.
If your guess is too low, you should be told!
If your guess is too high, you should be told!
The computer should keep track of how many guesses you have had.
If you guess lower than your previous guess when you were told the number is higher, the computer should say “I said the number is higher!”
If you guess higher than your previous guess when you were told the number is lower, the computer should say “I said the number is lower!”.

请问最后两个要求如何在程序中实现,题目给出的提示是使用这个
int generateRandom(int max){
return rand() % max + 1;
}
但是我不懂这个怎么用TT..希望能给出这个代码实例教教我,我是初学者小白~
(我一共就11财富值...各位见谅呀)
展开
 我来答
哥德堡快递
2016-08-12 · TA获得超过1581个赞
知道小有建树答主
回答量:1039
采纳率:68%
帮助的人:356万
展开全部
#include <iostream>

int generateRandom(int max) { return rand() % max + 1; }
int main()
{
int N = generateRandom(100);
int input, lastInput = 0, choice = 0, count = 0;
cout << "猜吧猜吧(1-100):";
while (true)
{
cin >> input;
count++;
if (input > 100 || input < 1)
{
cout << "逗比,猜1-100的数,再来:";
continue;
}
if (input > N)
{
if (input > lastInput && choice == 1) cout << "尼玛,告诉你大了还往大猜,再来:";
else
{
cout << "太大了,再来:";
lastInput = input;
}
choice = 1;
}
else if (input < N)
{
if (input < lastInput && choice == 2) cout << "尼玛,告诉你小了还往小猜,再来:";
else
{
cout << "太小了,再来:";
lastInput = input;
}
choice = 2;
}
else
{
cout << "我靠,猜了" << count << "次,猜对了!" << endl;
break;
}
}
return 0;
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式