还没有增加时:
//猜1 - 100之间的随机数
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
char continueGame = 'y';
int systemNum, guessNum;
int guessTimes;
while(continueGame == 'y')
{
srand(time(0)); //time(0):当前时间
systemNum = rand() % 100 + 1;
guessTimes = 0;
do
{
cout << "Please input a number (1 - 100):";
cin >> guessNum;
guessTimes ++;
if(guessNum > systemNum)
{
cout << "Big!" << endl;
}
else
{
if(guessNum < systemNum)
{
cout << "Small!" << endl;
}
}
}while(systemNum != guessNum);
cout << "Congratulations! You have guessed " << guessTimes << "times!" << endl;
cout << "Press \'y\' to contune this game or other keys to finish!" << endl;
cin >> continueGame;
}
return 0;
}
增加后:
//猜1 - 100之间的随机数
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int main()
{
char continueGame1 = 'a';
char continueGame2 = 'b';
char continueGame3 = 'c';
int systemNum, guessNum;
int guessTimes;
string a;
string Simple="Simple";
string General="General";
string Difficult="Difficult";
cout <<"please choose Difficult General or Simple\n" ;
while(cin>>a)
{
if(a==Simple)
{
cout<<"You have many opportunities\n";
while(continueGame1 == 'a')
{
srand(time(0)); //time(0):当前时间
systemNum = rand() % 100 + 1;
guessTimes = 1;
do
{
cout << "Please input a number (1 - 100):";
cin >> guessNum;
if(guessNum > systemNum)
{
cout << "Big!" << endl;
guessTimes++;
}
else
{
if(guessNum < systemNum)
{
guessTimes++;
cout << "Small!" << endl;
}
}
}while(systemNum != guessNum);
cout << "Congratulations! You have guessed " << guessTimes << "times!" << endl;
cout << "Press \'a\' to contune this game or other keys to finish!" << endl;
cin >> continueGame1;
}
}
if(a==General)
{
cout <<"You only have seven chances\n";
while(continueGame2 == 'b')
{
srand(time(0)); //time(0):当前时间
systemNum = rand() % 100 + 1;
guessTimes = 1;
do
{
cout << "Please input a number (1 - 100):";
cin >> guessNum;
if(systemNum == guessNum)
{
cout << "Congratulations! You have guessed " << guessTimes << "times!" << endl;
break;
}
if(guessTimes>6)
{
cout<<"You lose\n";
break;
cout << "Press \'b\' to contiune this game or other keys to finish!" << endl;
}
if(guessNum > systemNum)
{
cout << "Big!" << endl;
guessTimes ++;
}
else
{
if(guessNum < systemNum)
{
cout << "Small!" << endl;
guessTimes ++;
}
}
}while(systemNum != guessNum);
cout << "Press \'b\' to contune this game or other keys to finish!" << endl;
cin >> continueGame2;
}
}
if(a==Difficult)
{
cout <<"You only have three chances\n";
while(continueGame3 == 'c')
{
srand(time(0)); //time(0):当前时间
systemNum = rand() % 100 + 1;
guessTimes = 1;
do
{
cout << "Please input a number (1 - 100):";
cin >> guessNum;
if(systemNum == guessNum)
{
cout << "Congratulations! You have guessed " << guessTimes << "times!" << endl;
break;}
if(guessTimes>2)
{
cout<<"You lose\n";
break;
cout << "Press \'c\' to contune this game or other keys to finish!" << endl;
}
if(guessNum > systemNum)
{
cout << "Big!" << endl;
guessTimes ++;
}
else
{
if(guessNum < systemNum)
{
cout << "Small!" << endl;
guessTimes ++;
}
}
}while(systemNum != guessNum);
cout << "Press \'c\' to contune this game or other keys to finish!" << endl;
cin >> continueGame3;
}
}
}
return 0;
}
照着老师发的模板填了一些东西,改了好久,一直都会在最后第7次的时候输和赢的结果一起出现后来把比较放在前面改过来了