提问者:小点点

如何在控制台C++中创建bool循环?


所以最近我觉得很无聊,我试图用C++在控制台中做一个简单的游戏。 一切都很完美,但是我不能让bool“fishing”循环,它只是在第一次“fishing”会话后返回0,有人能帮我吗? 这是我的代码

 fishing = false;

            if (fishing == false) {
                system("cls");
                cout << "Do you wish to fish again?" << endl;
                cout << "Current Amount of fish: ";
                cout << fish << endl;
                cin >> hellyea;
                if (hellyea == 1)
                    fishing = true;
            }

            while (fishing == true) {

                // the "waiting to catch a fish" part


                // randomly selecting type of fishes
                system("cls");
                srand(time(NULL));
                auto fishtype = rand() % 3;

                if (fishtype == 0) {
                    system("cls");
                    cout << "You just got an: " << "Common Carp!" << endl;
                    fish++;
                    cout << "Your Fish Ammount: ";
                    SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
                    cout << fish << endl;
                    SetConsoleTextAttribute(hConsole, saved_colors);
                    Sleep(2000);

                }
                if (fishtype == 1) {
                    system("cls");
                    cout << "Argh! You just found: " << "A bag Of Trash!" << endl;
                    cout << "You lose 1 fish :(" << endl;
                    fish = fish - 1;
                    if (fish == -1)
                        fish++;
                    cout << "Your Fish Ammount: ";
                    SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
                    cout << fish << endl;
                    SetConsoleTextAttribute(hConsole, saved_colors);
                    Sleep(2000);
                }
                if (fishtype == 2) {
                    system("cls");
                    cout << "You just got an: " << "Gold Fish! Congrats!";
                    fish++;
                    cout << "Your Fish Ammount: ";
                    SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
                    cout << fish << endl;
                    SetConsoleTextAttribute(hConsole, saved_colors);
                    Sleep(2000);
                }
                fishing = false;
            }

有人能帮我一下吗?


共3个答案

匿名用户

因为你告诉它:

fishing = false;

就在循环的末端。

当然,在此之后检查fishing==true将会失败。

匿名用户

如果你想继续捕鱼后,捕到一条鱼,你将需要把“你想再次捕鱼吗?” 在一个单独的for/while循环中,以便在抓到鱼之后再次询问问题。

匿名用户

在while循环结束时,使fishing=false循环结束,因为fishing==true返回0

fishing = false;

            if (fishing == false) {
                system("cls");
                cout << "Do you wish to fish again?" << endl;
                cout << "Current Amount of fish: ";
                cout << fish << endl;
                cin >> hellyea;
                if (hellyea == 1)
                    fishing = true;
            }

            while (fishing == true) {

                // the "waiting to catch a fish" part


                // randomly selecting type of fishes
                system("cls");
                srand(time(NULL));
                auto iSecret = rand() % 3;

                if (iSecret == 0) {
                    system("cls");
                    cout << "You just got an: " << "Common Carp!" << endl;
                    fish++;
                    cout << "Your Fish Ammount: ";
                    SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
                    cout << fish << endl;
                    SetConsoleTextAttribute(hConsole, saved_colors);
                    Sleep(2000);

                }
                if (iSecret == 1) {
                    system("cls");
                    cout << "Argh! You just found: " << "A bag Of Trash!" << endl;
                    cout << "You lose 1 fish :(" << endl;
                    fish = fish - 1;
                    if (fish == -1)
                        fish++;
                    cout << "Your Fish Ammount: ";
                    SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
                    cout << fish << endl;
                    SetConsoleTextAttribute(hConsole, saved_colors);
                    Sleep(2000);
                }
                if (iSecret == 2) {
                    system("cls");
                    cout << "You just got an: " << "Gold Fish! Congrats!";
                    fish++;
                    cout << "Your Fish Ammount: ";
                    SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
                    cout << fish << endl;
                    SetConsoleTextAttribute(hConsole, saved_colors);
                    Sleep(2000);
                }
            }

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(何在|控制台|c++|中|创建|bool|循环)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?