如何添加计数器

时间:2015-04-18 19:18:31

标签: c++ loops counting

我对如何在我的代码中添加一个计数器感到困惑 - 我正在编写一个2人游戏数字猜测游戏,我想添加为满足程序所需的猜测次数。

这是我到目前为止所得到的:

#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{

    int x;
    int  guess;

    std::cout << "Enter the number for the player to guess.";
    std::cin >> x;

    do
    {
        std::cout << "Enter your guess.";
        std::cin >> guess;
        if (guess > x)
            std::cout << "lower\nEnter your guess.\n";
        else if (guess < x)
            std::cout << "higher\nEnter your guess.\n";
        else
            std::cout << "You guessed it!\n";
    } while (guess != x);

    return 0;
}

4 个答案:

答案 0 :(得分:2)

你只需要添加一个变量并在每次循环时增加它,如下所示:

int guesses = 0;

do
{
    guesses++;
    std::cout << "Enter your guess.";
    std::cin >> guess;
    if (guess > x)
        std::cout << "lower\nEnter your guess.\n";
    else if (guess < x)
        std::cout << "higher\nEnter your guess.\n";
    else
        std::cout << "You guessed it!\n";
} while (guess != x);

std::cout << "You took " << guesses << " guesses!" << std::endl;

答案 1 :(得分:0)

  #include <iostream>
#include <cstdlib>
#include <ctime>


 int main()

{

int x;
int  guess;

std::cout << "Enter the number for the player to guess.";
std::cin >> x;
int count = 0;    

  do
{
    std::cout << "Enter your guess.";
    std::cin >> guess;
    if (guess > x)
        std::cout << "lower\nEnter your guess.\n";
    else if (guess < x)
        std::cout << "higher\nEnter your guess.\n";
    else
        std::cout << "You guessed it...";
    count++;
  } while (guess != x);
  std::cout << "in " << count << "times" <<endl; 

  return 0;
 }

计数变量将计算玩家猜测的次数,并且您可以在猜测正确后打印计数。

答案 2 :(得分:0)

使用变量存储猜测次数并递增,直到答案匹配。 :)

#include <iostream>
#include <cstdlib>
#include <ctime>


 int main()

{

int x;
int  guess;

std::cout << "Enter the number for the player to guess.";
std::cin >> x;
int counter=0;
  do
{
    counter++;
    std::cout << "Enter your guess.";
    std::cin >> guess;
    if (guess > x)
        std::cout << "lower\nEnter your guess.\n";
    else if (guess < x)
        std::cout << "higher\nEnter your guess.\n";
    else
        std::cout << "You guessed it!\n";
  } while (guess != x);
std::cout<<"You have guessed "<<counter<<" times for correct answer"<<endl;
  return 0;
 }

答案 3 :(得分:0)

#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{

    int x;
    int guess;

    std::cout << "Enter the number for the player to guess.";
    std::cin >> x;

    int counter = 0; //make a new variable to count the guesses
    do
    {
        std::cout << "Enter your guess.";
        std::cin >> guess;
        if (guess > x)
            std::cout << "lower\nEnter your guess.\n";
        else if (guess < x)
            std::cout << "higher\nEnter your guess.\n";
        else
            std::cout << "You guessed it!\n";
        ++counter; //increase it each time a guess happened
    } while (guess != x);

    //print the count of guesses
    std::cout << "Took you " << counter << " guesses!" << std::endl;

    return 0;
}