当前位置:  开发笔记 > 编程语言 > 正文

清理多个if语句C++

如何解决《清理多个if语句C++》经验,为你挑选了2个好方法。

我正在做"Udemy - 通过开发你的第一个游戏来学习用C++编写代码",这是一个虚幻的引擎C++基础课程,在这个课程中你开发了一个用户试图猜测一个单词的小游戏.

代码效果很好.但我想加入它.我在下面创建了代码并且效果很好.但它是UGGGGLLLY.由于我还处于早期学习阶段,我想开始养成正确的习惯.

所以问题是如何使所有这些If语句消失或凝结.如果我想再添加50个单词,我不想再创建50个if语句.

我试图将ISecret更改为FString并使用该数字从HIDDEN_WORD [1]获取数字,但它没有按计划工作.

这就是我的想法:

ISecret[1-100] = MyHiddenWord[1-100] = HIDDEN_WORD[1-100]

我知道这不起作用,我知道我必须在银行中列出"单词",但是我可以创建一个单词库并列出银行中的所有单词吗?

    int32 ISecret;             //This section generates a 
    srand(time(NULL));        // random number between 1 and 10.
    ISecret = rand() % 10, 1;///

    const FString HIDDEN_WORD01 = "planet";
    const FString HIDDEN_WORD02 = "bait";
    const FString HIDDEN_WORD03 = "dog";
    const FString HIDDEN_WORD04 = "cat";
    const FString HIDDEN_WORD05 = "stream";///  These are the Hidden words
    const FString HIDDEN_WORD06 = "taco";
    const FString HIDDEN_WORD07 = "ship";
    const FString HIDDEN_WORD08 = "balcony";
    const FString HIDDEN_WORD09 = "tail";
    const FString HIDDEN_WORD10 = "barf";

         if (ISecret == 1){MyHiddenWord = HIDDEN_WORD01;}
    else if (ISecret == 2){MyHiddenWord = HIDDEN_WORD02;}
    else if (ISecret == 3){MyHiddenWord = HIDDEN_WORD03;}// These make is so
    else if (ISecret == 4){MyHiddenWord = HIDDEN_WORD04;}//what ever number 
    else if (ISecret == 5){MyHiddenWord = HIDDEN_WORD05;}//is randomly 
    else if (ISecret == 6){MyHiddenWord = HIDDEN_WORD06;}//generated that  
    else if (ISecret == 7){MyHiddenWord = HIDDEN_WORD07;}//the correct
    else if (ISecret == 8){MyHiddenWord = HIDDEN_WORD08;}//HIDDEN_WORD
    else if (ISecret == 9){MyHiddenWord = HIDDEN_WORD09;}//is chosen.  
    else if (ISecret == 10){MyHiddenWord = HIDDEN_WORD10;}

Alex Zywicki.. 7

您可以将它们存储在std::array:

#include

const std::array hidden_words =
{
    "planet",
    "bait",
    "dog",
    "cat",
    "stream",
    "taco",
    "ship",
    "balcony",
    "tail",
    "barf"
};

int main()
{
    int ISecret = 0;
    std::cout<

或者a std::vector

通常,如果您计划使用整数来区分每个元素,则将元素存储在数组中会很有用.



1> Alex Zywicki..:

您可以将它们存储在std::array:

#include

const std::array hidden_words =
{
    "planet",
    "bait",
    "dog",
    "cat",
    "stream",
    "taco",
    "ship",
    "balcony",
    "tail",
    "barf"
};

int main()
{
    int ISecret = 0;
    std::cout<

或者a std::vector

通常,如果您计划使用整数来区分每个元素,则将元素存储在数组中会很有用.



2> melpomene..:

首先,

ISecret = rand() % 10, 1;

是错的.此语句等效于ISecret = rand() % 10;1无效(这应触发编译器警告).

如果你想要一个[1,10](含)范围内的随机数,你应该这样做:

ISecret = rand() % 10 + 1;

要为给定数字选择单词,最简单的方法可能是数组:

const FString hidden_word[] = {
    "planet",
    "bait",
    "dog",
    "cat",
    "stream",
    "taco",
    "ship",
    "balcony",
    "tail",
    "barf"
};
ISecret = rand() % 10;  // a number between 0 and 9
MyHiddenWord = hidden_word[ISecret];

推荐阅读
惬听风吟jyy_802
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有