> =并且<=不按照我使用它们的方式工作,我在哪里弄糟? [处于保留状态]

问题描述 投票:2回答:1

我的完整代码

#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.从我的角度来看,这应该有效,但是在逻辑上(对我而言)不可行的情况下,只有一半有效。

c++
1个回答
0
投票

您有一个无限循环:

while (ComputerBot.Health >= 0 && UserBot.Health >= 0);
© www.soinside.com 2019 - 2024. All rights reserved.