不显示列表中选定的选项

Not displaying a chosen selection from a list?

本文关键字:选项 显示 列表      更新时间:2023-10-16

所以我被要求在我的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;

}

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

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作为用户输入。