用c++编写猜数字的游戏 请高手指教 要求电脑输出随机数字 让用户来猜,程序提供选择难度 重新选择的功能。

2024年11月20日 04:46
有3个网友回答
网友(1):

发现有3个问题:1 if(choice=='l'||'L')语句判断必为真,该成if((choice=='l')||(choice=='L'))。
2.简单难度的最后多加了一个break
3.h难度下int count语句应该定在do while循环外面

#include
#include
#include
#include
#include
using namespace std;
int main ()
{
cout<<"GUESS NUMBER"< cout<<"select your difficult level"< cout<<"l:Low difficult---numbers from 1 to 10 \n"< cout<<"m:Middle difficult---numbers from 1 to 50 \n"< cout<<"h:High Difficult---:numbers from 1 to 100 \n"< cout<<"Enter 2:quit from game."< char choice;
int iSecret, iGuess;
cout<<"Please select your difficulty with enther l,m,or h)";
while(1)
{
cin>>choice;
int count=0;
if((choice=='l')||(choice=='L'))
{
//initialize random seed:
srand ( time(NULL) );
// generate secret number:
iSecret = rand() % 10 + 1;

do{
count++;
cout<<"Guess the number from 1 to 10. \n";
cin>>iGuess;
if(iSecret cout<<"The secret number is lower. \n";
else if (iSecret>iGuess)
cout<<"The secret number is higher. \n";
else if (iSecret=iGuess)
{
cout<<"Congratulation."< break;
}
if(count==3)
{
// quit from while loop.
cout<<"Only 3 times, please select level. \n";
break;
}
}while(iSecret!=iGuess);
cout<<"Please select level again:"< //cout<<"Congratulation.You will."< }

if((choice=='m')||(choice=='M'))
{
srand ( time(NULL) );
iSecret = rand() % 50 + 1;
int count=0;
do{
cout<<"Guess the number from 1 to 50. \n";
cin>>iGuess;
count++;
if(iSecret cout<<"The secret number is lower. \n";
else if (iSecret>iGuess)
cout<<"The secret number is higher. \n";
else if(iSecret=iGuess)
{
cout<<"Congratulation."< cout<<"Please select level again."< break;
}
if(count==3)
{
cout<<"Only 3 times, please select level."< break;
}
}while(iSecret!=iGuess);
cout<<"Please select level again:"< }

if((choice=='h')||(choice=='H'))
{
//initialize random seed:
srand ( time(NULL) );
// generate secret number:
iSecret = rand() % 100 + 1;
int count=0;
do
{
cout<<"Guess the number from 1 to 100. \n";
cin>>iGuess;
count++;
if(iSecret cout<<"The secret number is lower. \n";
else if (iSecret>iGuess)
cout<<"The secret number is higher. \n";
else if(iSecret=iGuess)
{
cout<<"Congratulation. \n";
cout<<"Please select level again. \n";
break;
}
if(count==3)
{
cout<<"Only 3 times, please select level. \n";
break;
}
}while(iSecret!=iGuess);
cout<<"Please select level again:"< }
}
return 0;
}

网友(2):

没改你的,按你的要求重新写了一个
自己研究研究吧
#include "iostream.h"
#include "time.h"
#include "stdlib.h"

int levelChoice(int a)
{
int key;
srand((unsigned int)time(NULL));
key=rand()%a+1;
return key;
}

int compare(int key)
{
int i,a;
for(i=0;i<3;i++)
{
cout<<"请输入你要猜的数字:";
cin>>a;
if(a==key)
{
return 1;
}
else
{
cout<<"对不起猜错了,你还剩"<<2-i<<"次机会"< }
}
return 0;
}

int main()
{
for(;;)
{
system("cls");
int key; //存放随机的值
int result;
char interrupt;
cout<<"\t\t猜数字游戏"< cout<<"1. 1-10的范围"< cout<<"2. 1-50的范围"< cout<<"3. 1-100的范围"< cout<<"4.退出本游戏"< cout<<"\n请进行选择:";
char choice;
cin>>choice;
switch(choice)
{
case '1':key=levelChoice(10);
result=compare(key);
if(result==1)
{
cout<<"恭喜你答对了!输入任意的值继续……"< cin>>interrupt;
}
else
{
cout<<"对不起,三次都没答对,正确结果为"< cin>>interrupt;
}
break;
case '2':key=levelChoice(50);
result=compare(key);
if(result==1)
{
cout<<"恭喜你答对了!输入任意的值继续……"< cin>>interrupt;
}
else
{
cout<<"对不起,三次都没答对,正确结果为"< cin>>interrupt;
}
break;
case '3':key=levelChoice(100);
result=compare(key);
if(result==1)
{
cout<<"恭喜你答对了!输入任意的值继续……"< cin>>interrupt;
}
else
{
cout<<"对不起,三次都没答对,正确结果为"< cin>>interrupt;
}
break;
case '4':return 0;
default:
cout<<"输入错误,请输入任意的值重新进行选择……"< cin>>interrupt;
}
}
}

网友(3):

#include
#include
using namespace std;

void PrintChoice()
{
cout << "*******************GUESS---NUMBER**********************" << endl
<< "* L:Low difficult --- numbers from 1 to 10 *" << endl
<< "* M:Middle difficult --- numbers form 1 to 50 *" << endl
<< "* H:High difficult --- numbers form 1 to 100 *" << endl
<< "* Q:Quit from game *" << endl
<< "******************************************************" << endl
<< endl
<< "Please Input Your Choice" << endl;
}
bool GuessNumber(int Secret,int Level)
{
bool IsGuessed = false;
int IGuess = 0;

for( int i = 0; i < 3 ; i++)
{
cout << "Guess the number from 1 to " << Level << "." << endl;
cin >> IGuess;
if(IGuess == Secret)
{
cout << "Congratulation." << endl;
return true;
}
else if(IGuess < Secret)
{
cout << IGuess << "is lower." << endl;
}
else if(IGuess > Secret)
{
cout << IGuess << "is higher." << endl;
}
}
cout << "Only 3 times. The number is " << Secret << "." << endl;
return false;
}
int main()
{
bool IsQuit = false;
int Secret;
bool ValidCammand;
srand(time(NULL));
int Level = 10;
while(!IsQuit)
{
char Choice;
PrintChoice();
cin >> Choice;
ValidCammand = false;
switch(Choice)
{
case 'l':;
case 'L':Secret = rand()% 10 + 1;Level = 10 ;break;
case 'm':;
case 'M':Secret = rand()% 50 + 1;Level = 50 ;break;
case 'h':;
case 'H':Secret = rand()% 100 + 1;Level = 100;break;
case 'q':;
case 'Q':IsQuit=true;break;
default:ValidCammand = true;break;
}
if(!IsQuit && !ValidCammand)
{
GuessNumber(Secret,Level);
}
}
}