C ++ - 我正在编写我的第一个程序,但是遇到了一些麻烦

时间:2016-11-20 16:35:04

标签: c++

我最近开始学习C ++并希望制作我的第一个“游戏”/程序。我遇到了一些困难。

到目前为止我的错误如下:

  1. (rand()%a) - >更改“a”不会执行任何操作(例如,如果生成的数字为2且“a”为1,则生成的数字保持为2)。

  2. 以下代码不起作用:

    while(!(b = c)){
        cout << "Enter your guess! \n";
        cin >> c;
        if(c<b){
            cout << "Bigger! \n";
        }
        if(c>b){
            cout << "Smaller! \n";
        }
        d++;
    }
    
  3. 我的完整程序如下:

    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    
    int main()
    {
        // Max. Limit
        int a;
        // Random Number
        int b;
        // Guess
        int c;
        // Tries counter
        int d = 0;
    
        cout << "Enter highest possible number, setting the max. limit for the program. \n";
        cin >> a;
    
        srand(time(0));
        b = 0 + (rand()%a);
    
        if(b =! 1){
            c = 1;
        }
        if(b = 1){
            c = 2;
        }
        while(!(b = c)){
            cout << "Enter your guess! \n";
            cin >> c;
    
            if(c<b){
                cout << "Bigger! \n";
            }
            if(c>b){
                cout << "Smaller! \n";
            }
            d++;
        }
        if(b=c){
            cout << "Congratulations! You have guessed was right! The number was indeed " << b << " !" << endl;
            cout << "You needed " << d << " tries to find the number! \n";
        }
        return 0;
    }
    

2 个答案:

答案 0 :(得分:3)

好吧,你需要知道的第一件事是(正如drescherjm已经指出的那样),b = c不是你想要的。相反,您需要b == c进行比较。 另一件事是:

  if(b =! 1){
        c = 1;
    }
    if(b = 1){
        c = 2;
    }

通过将c替换为b,您可以避免将while-loop初始化为与do-while-loop不同的值。如果您还摆脱了using namespace std;并使用<random>而不是rand(),请将您的短变量(ab)重命名为他们实际正在执行的操作,你的代码变得更清晰,更现代。

#include <iostream>
#include <random>

int main()
{
    int max_limit{ 0 };
    int random_number{ 0 };
    int guess{ 0 };
    int number_of_guesses{ 0 };

    std::cout << "Enter highest possible number, setting the max. limit for the program. \n";
    std::cin >> max_limit;


    std::random_device now;
    std::mt19937 engine(now()); //random seed
    std::uniform_int_distribution<int> r(0, max_limit); //range

    random_number = r(engine);


    do{
        std::cout << "Enter your guess! \n";
        std::cin >> guess;

        if (guess<random_number){
            std::cout << "Bigger! \n";
        }
        if (guess>random_number){
            std::cout << "Smaller! \n";
        }
        number_of_guesses++;
    } while (random_number != guess); //do the code above until this is false

    std::cout << "Congratulations! Your guess was right! The number was indeed " << random_number << " !" << std::endl;
    std::cout << "You needed " << number_of_guesses << " tries to find the number! \n";

    return 0;
} 

示例运行:

Enter highest possible number, setting the max. limit for the program.
100
Enter your guess!
50
Smaller!
Enter your guess!
25
Smaller!
Enter your guess!
10
Bigger!
Enter your guess!
15
Congratulations! Your guess was right! The number was indeed 15 !
You needed 4 tries to find the number!

所以是的,那是有效的。

答案 1 :(得分:0)

a = ba分配b的值 a == b比较两个变量的de值。 关于第1个问题,关于种子检查。