C++ if 语句不返回

C++ if statements not returning

本文关键字:返回 语句 if C++      更新时间:2023-10-16
int main()
{
char command = 'a';
Monster Goblin;Goblin.HP = 5;Goblin.name = "Goblin";
if(command == 'a'){
    cout<<"At the main Menu, what to do now? Enter H for a list of commands!"<< endl;
    cin>>command;
    switch(command)
    {
        case 'a':
            cout<<"Going to the main menu!"<<endl;
            command = 'a';
            break;
        case 'b':
            cout<<"Going to command line B"<<endl;
            command = 'b';
            break;
        case 'c':
            cout<<"going to command line C"<<endl;
            command = 'c';
            break;
        }
}
if(command == 'b')
{
    cout<<"You made it to command line B"<<endl;
    cout<<"Now lets try to make it go back to the MM!"<<endl;
    command = 'a';
}
if (command == 'c')
{
    cout<<"You made it to command line C"<<endl;
}
}

当我输入 b 时,我试图让它进入,它将输出到命令行 B 和其他两行,然后返回主菜单,即"a",如果命令 char 等于"a",为什么它不返回主菜单?

好吧,

没有什么告诉你的代码在第一个条件下返回。

您可以执行以下操作:

while(true)
{
if(command == 'a'){
    cout<<"At the main Menu, what to do now? Enter H for a list of commands!"<< endl;
    cin>>command;
    switch(command)
    {
        case 'a':
            cout<<"Going to the main menu!"<<endl;
            command = 'a';
            break;
        case 'b':
            cout<<"Going to command line B"<<endl;
            command = 'b';
            break;
        case 'c':
            cout<<"going to command line C"<<endl;
            command = 'c';
            break;
        }
}
if(command == 'b')
{
    cout<<"You made it to command line B"<<endl;
    cout<<"Now lets try to make it go back to the MM!"<<endl;
    command = 'a';
}
if (command == 'c')
{
    cout<<"You made it to command line C"<<endl;
}
}

使用switch

switch (command) {
case 'a':
    ...
    break;
case 'b':
    ...
    break;
case 'c':
    ...
    break;
default: 
    break;
}

如果您的主菜单只是顶部的"cout",然后是另一个 cin,那么有两种解决方案。

您可能一直在考虑的解决方案是将代码示例包装在 while 循环中。

(while command != "c"){ ... }

这将在您选择 c 后结束代码。我假设如果玩家特别选择 c,您不想返回主菜单。

但是,对于当前的代码,我对这种方法并不感到兴奋,因为循环将继续检查更新的command变量。如果您不想无限循环相同的命令,则必须为每个命令设置command的状态为"a"。更好的解决方案是将代码分离到函数中。

举个例子:

void commandB (){
    cout<<"You made it to command line B"<<endl;
    cout<<"Now lets try to make it go back to the MM!"<<endl;
}
void commandC (){
     cout<<"You made it to command line C"<<endl;
}

int main()
{
    char command = 'a';
    //Monster Goblin;Goblin.HP = 5;Goblin.name = "Goblin";
    while (command != 'c'){
        if(command == 'a'){
            cout<<"At the main Menu, what to do now? Enter H for a list of commands!"<< endl;
            cin>>command;
            switch(command)
            {
                case 'a':
                    cout<<"Going to the main menu!"<<endl;
                    break;
                case 'b':
                    cout<<"Going to command line B"<<endl;
                    commandB(); 
                    command = 'a'; // THIS IS WHAT KEEPS YOU WITHIN THE MM!
                    break;
                case 'c':
                    cout<<"going to command line C"<<endl;
                    commandC();
                    break;
            }
        }
    }
} 

附言:如果变量已经是变量"a",则没有理由将命令更改为变量"a":

(您的代码)

case 'a':
            cout<<"Going to the main menu!"<<endl;
            command = 'a';
            break;

这样的东西可能被认为是良好而清晰的编程风格:

// Function forward-declarations:
void EnterMainMenu();
void EnterCommandLineB();
void EnterCommandLineC();
int main()
{
    Monster Goblin;
    Goblin.HP = 5;
    Goblin.name = "Goblin";
    EnterMainMenu();
}
void EnterMainMenu()
{
    while(true) // Infinite loop
    {
        char command;
        cout<<"At the main Menu, what to do now? Enter H for a list of commands!"<< endl;
        cin>>command;
        switch(command)
        {
            case 'a':
                cout<<"Going to the main menu!"<<endl;
                // Main menu loop will start again after the next line
                break;
            case 'b':
                cout<<"Going to command line B"<<endl;
                EnterCommandLineB();
                break;
            case 'c':
                cout<<"going to command line C"<<endl;
                EnterCommandLineC();
                break;
        }
    }
}
void EnterCommandLineB()
{
    cout<<"You made it to command line B"<<endl;
    cout<<"Now lets try to make it go back to the MM!"<<endl;
}
void EnterCommandLineC()
{
    cout<<"You made it to command line C"<<endl;
}

请注意,在 switch 语句之后,循环将从头开始,包括当EnterCommandLine函数完成执行时。