所以最近我觉得很无聊,我试图用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;
}
有人能帮我一下吗?
因为你告诉它:
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);
}
}