Closed. This question is not reproducible or was caused by typos。它当前不接受答案。
想改善这个问题吗?更新问题,以便将其作为on-topic用于堆栈溢出。
去年关闭。
Improve this question
我的完整代码
我有麻烦
我也这样尝试过
奇怪的是,当我尝试这样
我用||尝试了几次运算符而不是&&,但也无济于事,我还尝试使用断点并在程序到达while循环时查看
接下来是代码块,您想删除该
想改善这个问题吗?更新问题,以便将其作为on-topic用于堆栈溢出。
去年关闭。
Improve this question
我的完整代码
#include <iostream>
#include <time.h>
using namespace std;
struct Bot
{
int Health, Wins, Losses;
string Name;
};
int main()
{
Bot UserBot;
Bot ComputerBot;
string Answer;
bool PlayAgain = true;
int Turns = 1;
// Sets random seed to time(0)
srand(time(0));
// A list of names that the computer bot can be assigned
string ComputerNames [10] = {"Jeff", "Frank", "Bob", "Joseph", "BobsYourUncle", "Stacy", "Isabelle", "Evelyn", "Liam", "Emma"};
// User assignes bot name here
string UserBotName;
cout << " What is your robot's name?\n ";
cin >> UserBotName;
// Needs to be outide loop so they dont rest to zero
UserBot.Wins = 0;
UserBot.Losses = 0;
// Main game loop
do {
// Computer Bot settings
ComputerBot.Health = 100;
ComputerBot.Losses = rand() % 10 + 1;
ComputerBot.Wins = rand() % 10 + 1;
ComputerBot.Name = ComputerNames[rand() % 10];
// User Bot settings
UserBot.Health = 100;
UserBot.Name = UserBotName;
// NEED HELP HERE
// WONT ENTER LOOP, WHY?
while (ComputerBot.Health >= 0 && UserBot.Health >= 0);
{
if (Turns == 1)
{
cout << " Computers Turn\n";
Turns *= -1;
}
else if (Turns == -1)
{
cout << " Users Turn\n";
Turns *= -1;
}
system("pause");
}
// Asks the User id they want to play agin.
cout << " Do you want to play again? (Any answer besides n/N or no/No are counted as yes)/n ";
cin >> Answer;
// Sets PlayAgain to false and exits the game, else the game goes on.
if (Answer == "n" || Answer == "N" || Answer == "no" || Answer == "No")
{
PlayAgain = false;
}
} while (PlayAgain == true);
return 0;
}
我有麻烦
while (ComputerBot.Health >= 0 && UserBot.Health >= 0);
ComputerBot.Health和Userbot.Health都== 100,但是该程序未进入循环。我也这样尝试过
while (0 <= ComputerBot.Health && 0 <= UserBot.Health);
但这没有帮助奇怪的是,当我尝试这样
while (0 >= ComputerBot.Health && 0 >= UserBot.Health);
或while (ComputerBot.Health <= 0 && UserBot.Health <= 0);
即使.Health
的值没有变化,它也循环运行一次,并且比较是向后的。我用||尝试了几次运算符而不是&&,但也无济于事,我还尝试使用断点并在程序到达while循环时查看
.Health
变量的值,但它们仍为100,表示它们大于或等于0。从我的 Angular 来看,这应该起作用,但是在逻辑上(对我而言)不应该起作用的只有一半。 最佳答案
您有一个无限循环:
while (ComputerBot.Health >= 0 && UserBot.Health >= 0);
接下来是代码块,您想删除该
;
。关于c++ - > =和<=不按我使用它们的方式工作,我在哪里弄糟? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58349351/
10-10 04:50