-2
int main()
{
    //declare variables
    int randomNumber = 0;
    int numberGuess  = 0;
    int attempts     = 0;
    int startTime    = 0;
    int endTime           = 0;
    int totalSecond  = 0;
    int upperBound  =  0;
    char gameType   = ' ';
    char again      = 'Y';

    while(again == 'Y' || again == 'y')
    {
        cout <<" Game Type Enter B for Beginner,I for Intermediate, and A for Advanced:";
        cin >> gameType;
        gameType=toupper(gameType);

        if(gameType == 'B')
        {
            cout <<" Guess a number from 1 through 10:";

            upperBound = 10;

        }
        else if( gameType == 'I')
        {
            cout <<" Guess a number from 1 through 100:";

            upperBound = 100;
        }
        else if( gameType == 'A')
        {
            cout <<" Guess a number from 1 through 1000:";

            upperBound = 1000;
        }
        else 
            cout <<" Invalid Game Level:";
        cout << endl;

        //generate a random number

        srand(static_cast<int>(time(0)));
        randomNumber = 1 + rand() % ((upperBound) - 1 + 1);

        //get first number guess from user
        cin >> numberGuess;
        startTime = static_cast<int>(time(0));
        while ( numberGuess != randomNumber)
        {
            if( numberGuess > randomNumber)
            {
                cout <<"Sorry, guess lower:";
                cin >> numberGuess;
            }
            else if ( numberGuess < randomNumber)
            {
                cout <<"Sorry, guess higher:";
                cin >> numberGuess;
            }
            attempts+=1; 

        }
        cout << endl <<" Yes, the number is " << randomNumber <<"." << endl;
        endTime= static_cast<int>(time(0));
        totalSecond = endTime- startTime;
        cout << endl <<" This is how many attempts you took:" << attempts << endl;
        cout << endl <<" This took :" << totalSecond <<" seconds"<< endl;
        cout <<" Do you want to play again? (Y/N)";
        cin >> again;
        cout << endl << endl;
    } 

    system("pause");
    return 0; 
}

My attempts are adding all up together on each game why is this?? Like if I play one game it displays I took 5 attempts and when I play again say I took 3 attempts it will say I took 8 attempts why??Then it go on and on??

4

2 回答 2

3

因为您没有attempts在循环之间重置为零。相反,您在声明它时将其设置为零(int attempts = 0;),然后继续在循环中旋转而不做任何事情将其设置回零。您的所有其他变量同样不会重置,但大多数是在代码的设置阶段设置的。

于 2013-04-29T13:19:56.227 回答
1

attempts您必须在“新游戏”循环开始时重置数量。

 while(again == 'Y' || again == 'y')
   {
    attempts = 0;

不要在一个巨大的main()方法中实现完整的程序功能。将其拆分为您可以实现resetGameState()和设置的函数,以及将attempts其他游戏状态变量恢复为默认值。

从长远来看,最好了解类和面向对象编程的工作原理。在 OOP 中,您可以拥有一个包含etc.的Game类作为其成员变量,然后您可以像这样使用它:attemptsmain()

while (again == 'Y' || again == 'y')
{
    Game newGame = new Game(); // with attempts etc. set to defaults automatically
    newGame.start(); // this is Java syntax actually; you may have to
    // do it in a slightly different way in C++ but the concept is same

这是一篇关于C++ 对象和类的优秀文章,可以帮助您入门。

于 2013-04-29T13:23:08.250 回答