作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 C++ 的新手,我在分配时遇到了一些问题。到目前为止,这是我的程序:
#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 循环似乎没有初始化。直到程序的“空战”部分,一切都运行良好。我该如何纠正这个问题?
最佳答案
您是说 for (int x = arrowCount; x >= 0; x--) {
吗?请注意,for
循环条件检查是在循环体运行之前评估的。
目前,如果 x
为正,您的循环将终止,从您的变量名来看,这种情况最有可能发生。
另请注意,for
循环的主体既不依赖于 x
,也不依赖于 enemyCount
,这看起来很奇怪。
关于c++ - For 循环不在 void - fight 场景下运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39706265/
我是一名优秀的程序员,十分优秀!