题目:系统随机生成一个1到100之间的数字,玩家进行猜测
如果猜错,提示玩家数字过大或者过小
如果才对,恭喜玩家获得胜利
#include <iostream>
/*
猜数字游戏:系统随机生成一个1到100之间的数字,玩家进行猜测
如果猜错,提示玩家数字过大或者过小
如果才对,恭喜玩家获得胜利
*/
using namespace std;
int main() {
cout << "_______GAME GUESS NUMBER_______" << endl;
int guess;
int res = (rand() % 100); //随机生成一个1~100的随机数
do {
cout << "Please guess your answer : ";
cin >> guess;
if(res == guess) break;//猜对,跳出循环
if(res < guess) {
//答案比猜测小
cout << "The answer is SMALLER than your guess, ";
} else {
//答案比猜测大
cout << "The answer is LARGER than your guess, ";
}
cout << "please try again." << endl;
} while(res != guess);//答案与猜测不同,则继续循环
//答案与猜测相同,给出胜利信息
cout << "Your answer is right! The answer is : " << res << endl;
return 0;
}
注:此处生成的答案是伪随机数。