如何使用变量C ++输出某个数组

时间:2015-05-30 02:56:31

标签: c++ arrays

我希望数组输出一个特定的数组,具体取决于随机生成的变量是什么。

我希望有一个基于空手道的模拟器来读取对手的踢球(1,2,3或4)并将其与某些踢球联系起来。 (Round Kick,Hook Kick,Front Kick,Side Kick)然后将其打印到屏幕上。

如果需要,请跳至int main(),其余代码仅供参考。

我的代码:

#include <iostream>
#include <random>
#include <ctime>
#include <string>
#include <Windows.h>

using namespace std;

mt19937 ranGen(time(0));
uniform_real_distribution<float> attack(0.00f, 1.00f);
uniform_real_distribution<float> defense(0.00f, 1.00f);
uniform_int_distribution<int> okick(1, 4);

string belt = "White Belt";
float landed = attack(ranGen);
float blocked = defense(ranGen);
float ekick = okick(ranGen);
int kick;
string kicks [4] = ("Round Kick","Hook Kick","Front Kick","Side Kick");

void roundKick() {
if (belt == "White Belt") {
    if (landed < 0.70) {
        cout << "Round kick to head | Blocked\n";
    } else {
        cout << "Round kick to head | Direct Hit\n";
    }
}
}

int main()
{
string yn;

syn:
cout << "Spar? y/n\n";
cin >> yn;
if (yn == "y") {
    cout << "\nFighting Stance!\n";
    Sleep(3000);
    cout << "C'ject!\n\n";
    cout << "1 - Round Kick\n2 - Hook Kick\n3 - Front Kick\n4 - Side Kick\n";
    cin >> kick;
    if (kick == 1) {
        roundKick();
    }
    cout << "Opponent used kick " << ekick;
    if (blocked < 0.50) {
        //
        //
        //I want to replace the 1 with the random Gen kick (ekick) to have the user know which kick it is
        cout << kicks[1] << " to the body | Blocked\n";
    } else 
} else if (yn == "n") {
    cout << "This is a sparring simulator, kid. Hit yes.\n\n";
    goto syn;
}

return 0;
}

如果你不明白,只要告诉我你不会做什么,我就会尝试理解它。

1 个答案:

答案 0 :(得分:0)

我建议像:

    if(blocked < 0.50) {
        ekick = okick(ranGen);   // you had this in the code, but at  a wrong place
        cout << "Replied with " << kicks[ekick] << endl; 
    }

问题是你在错误的地方拥有所需语句的部分内容。您已经在游戏开始时定义并初始化了这些全局变量,因此它们在整个游戏中保持相同的值。

float landed = attack(ranGen);  
float blocked = defense(ranGen);
float ekick;  // we have now assigned ekick within the game. So here, just declare it

但我想你开始学习了。我们已经使用ekick解决了这个问题,但您仍然遇到landedblocked的类似问题。由你来解决它。 Senpai ni rei!

P.S:在你完成这个之后,你应该用一个while循环代替可怕的goto。后来,改进的一件好事就是摆脱全局变量