C++编程 猜数字

来源:百度知道 编辑:UC知道 时间:2024/06/21 09:42:32
1-10随机1个数字 有三次机会 1次猜对了结束 第一次猜错了 显示还有2次机会 然后如果才对结束 猜错的话还有一次机会 第三次猜对了 结束猜错了 显示你输了
最好用基本的代码 loop,if else 和 do while 之类的
例子:
情况 1:

Welcome to the Number Guessing Game!!

**********************************

Try to guess the number that has been generated.

Please enter a number between 1 and 10 (including 1 and 10):4

THAT IS CORRECT!! YOU WIN!!

Thank you for playing the Number Guessing Game!

情况 2:

Welcome to the Number Guessing Game!!

**********************************

Try to guess the number that has been generated.

Please enter a number between 1 and 10 (including 1 and 10):4

Sorry, that is not correct. Please try again. You have 2 more chances: 5

Sorry, that is not correct. Please try again. You have 1 more chance: 7

Sorry, you did not guess the number. You Lose!!

HAHAHAHAHAHAHAHAHAHAHAHAHAHA

情况

#include <time.h>
#include <iostream>
using namespace std;
int main()
{
int num,count=0,guessnum;
time_t t;
srand((unsigned)time(&t)); //初始化随机数发生器
num=rand()%10+1;
cout<<"Welcome to the Number Guessing Game!!"<<endl<<"**********************************"<<endl<<endl;
cout<<"Try to guess the number that has been generated."<<endl<<"Please enter a number between 1 and 10 (including 1 and 10):";
while (count<3){
cin>>guessnum;
if (guessnum==num) goto loop;
else if (count<2) cout<<"Sorry, that is not correct. Please try again. You have "<<2-count<<" more chances:";
count++;
}
cout<<"Sorry, you did not guess the number. You Lose!!"<<endl<<"HAHAHAHAHAHAHAHAHAHAHAHAHAHA";
return 0;