【问题标题】:Not displaying a chosen selection from a list?不显示从列表中选择的选项?
【发布时间】:2016-10-10 21:14:29
【问题描述】:

所以有人要求我在我的 C++ 课上制作游戏,我不久前上了 C++ 课,所以我有点生疏了。我记得你可以使用数组和列表?

游戏的重点是选择军队派系,选择军队类型,然后攻击某人。一旦我进入攻击部分,而不是从上一个菜单中检查所选派系的一堆 if 语句,我是否可以从程序的开头将派系实现到一个数组中,一旦我进入攻击阶段,运行通过数组并将用户选择的大陆与数组中的大陆进行比较,然后只保留匹配的输出?

如果有人可以发布一些代码或链接到提供此类示例的网站,将不胜感激,我想自己做,我并没有完全为我输入的东西。

#include <iostream>
using namespace std;

int main() {
//  Declare variables
int menuInput = 0, continentInput = 0, armyInput = 0, actionInput = 0;

//  Intro
cout << "WELCOME TO WAR" << endl;

//  Display main menu
do {
    cout << "1) Rules" << endl;
    cout << "2) Play Game" << endl;
    cout << "3) Quit" << endl;
    cout << "Menu choice: ";
    cin >> menuInput;


    //  if rules is selected
    if (menuInput == 1) {
        cout << endl;
        cout << "RULES: " << endl;
        cout << "1) Choose your player." << endl;
        cout << "2) Choose your army type." << endl;
        cout << "3) Choose to attack." << endl;
        cout << endl;
    }

    //  if game is selected
    else if (menuInput == 2) {
        cout << endl;
        cout << "START" << endl;

        //  first do while loop, continent choice
        do {
            cout << "1) North America" << endl;
            cout << "2) South America" << endl;
            cout << "3) Europe" << endl;
            cout << "4) Africa" << endl;
            cout << "5) Asia" << endl;
            cout << "6) Australia" << endl;
            cout << "7) Antartica" << endl;
            cout << "Choose your player from the list: ";
            cin >> continentInput;
            cout << endl;
            // invalid display if selection not in range
            if (continentInput <= 0 ||continentInput > 7) {
                cout << "INVALID" << endl;
                cout << endl;
            }
        } while (continentInput <= 0 || continentInput > 8);

        //  second do while loop, army type choice
        do {
            cout << "1) Army (Ground type forces)" << endl;
            cout << "2) Navy (Sea type forces)" << endl;
            cout << "3) Air Force (Air type forces)" << endl;
            cout << "Choose your army type from the list: ";
            cin >> armyInput;
            cout << endl;

            if (armyInput <= 0 || armyInput > 3) {
                cout << "INVALID" << endl;
            }
        } while (armyInput <= 0 || armyInput > 3);

        // third do while loop, who to attack


    }
    else if (menuInput == 3) {
        cout << endl;
        cout << "GAME OVER" << endl;
    }

    else {
        //  display invlaid input if number choice is not in given range
        cout << "INAVLID INPUT" << endl;
    }

} while (menuInput != 3);

return 0;

}

【问题讨论】:

  • 调试器是解决此类问题的正确工具。 询问 Stack Overflow 之前,您应该逐行浏览您的代码。如需更多帮助,请阅读How to debug small programs (by Eric Lippert)。至少,您应该 [编辑] 您的问题,以包含一个重现您的问题的 Minimal, Complete, and Verifiable 示例,以及您在调试器中所做的观察。
  • 你学过课程了吗?如果您有一个包含玩家和军队类型成员的类对象,您可以设置、获取并执行操作,这会更好。 tutorialspoint.com/cplusplus/cpp_classes_objects.htm
  • @DigitalNinja 类还没有达到那个点,第一个“版本”应该以更基本的方式编码,但这就是我在对象方面的计划定向的。

标签: c++ arrays arraylist text


【解决方案1】:

如果您将用户输入更改为数字,则可以引用一个填充了选项的数组。

military[x] = { "Army", "Navy" ...};
fromWhere[x] = { "North America", "South" ...};
toKill[x] = { "Whoever has oil" ... };
//get user input.
//toKill[get user input];
//kill with military[get user input];
//kill form fromWhere[get user input];
//do some attack logic 

只是提醒一下,您的问题相当模糊。你能澄清你需要什么。您似乎希望散列您的值并进行快速查找以避免字符串比较,但您不需要这样做,因为您正在获取 int 作为用户输入。

【讨论】:

  • 所以一旦我进入游戏的攻击阶段,我想要一个列表弹出你可以选择攻击的其他大陆,减去你选择的大陆,你“玩”的那个现在。所以我对如何用我现在的程序进行编码感到困惑。我在他们的大陆上有用户的输入,但由于其他选择不在程序中,我正在寻找有关将用户选择与列表进行比较的方法的输入。我将尝试你所说的,将选择列在列表中,并在我进入攻击菜单后运行 for 循环,也许这会奏效。
猜你喜欢
  • 1970-01-01
  • 2014-06-20
  • 1970-01-01
  • 2013-05-15
  • 1970-01-01
  • 2012-10-15
  • 2016-07-29
  • 1970-01-01
  • 2015-05-25
相关资源
最近更新 更多