首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >循环在无效战斗场景中不运行。

问循环在无效战斗场景中不运行。
EN

Stack Overflow用户
提问于 2016-09-26 15:05:37
回答 1查看 64关注 0票数 0

我是C++的新手,在一项任务上遇到了一些困难。到目前为止,我的计划如下:

代码语言:javascript
复制
#include "stdafx.h"

#include <iostream>
#include <string>
using namespace std;

void fight(string heroName, int arrowCount, int enemyCount);
int main(int argc, char** argv) {

    string heroName = "Legolas";
    int arrowCount = 3;
    int enemyCount = 3;

    cout << "Welcome to the Arena, the battle will begin shortly." << endl;
    system("pause");
    cout << "\nToday's competitors: " << "\n" << heroName << "\n" << "3 Orcs" << endl;

    system("pause");

    cout << "\n" << heroName << " will be given 3 arrows" << "\n" << "3 Orcs will be given daggers. \n";

    system("pause");

    cout << "\nThe battle begins in...\n 3...\n  2...\n   1..." << endl;

    system("pause");

    fight(heroName, arrowCount, enemyCount);
}

void fight(string heroName, int arrowCount, int enemyCount) {

    for (int x = arrowCount; x <= 0; x--) {

        if (arrowCount == 3) {
            cout << "\n" << heroName << " fires arrow at Azog." << "\nAzog has fallen due to lobotomy by arrow." << "\nLegolas notices he has 2 arrows remaining." << endl;
            system("pause");
        }

        if (arrowCount == 2) {
            cout << "\n" << heroName << " fires arrow at Dular." << "\nDular has taken an arrow to the knee and is now longer an adventurer." << "\nLegolas notices he has 1 arrow remaining." << endl;
            system("pause");
        }

        if (arrowCount == 1) {
            cout << "\n" << heroName << " fires arrow at Nagrub." << "\nNagrub has lost his testicles." << "\nLegolas notices he is out of Arrows." << endl;
            system("pause");
        }
    }
}

我遇到的问题是,for循环似乎没有初始化。一切运行良好,直到程序的“无效战斗”部分。我该怎么纠正呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-26 15:07:12

你是说for (int x = arrowCount; x >= 0; x--) {吗?注意,for循环条件检查是在运行循环体之前计算的。

目前,如果x是正的,则循环将终止,从变量名判断,情况很可能是这样。

还请注意,for循环的主体既不依赖于x,也不依赖于enemyCount,这似乎很奇怪。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39706265

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档