C++ if语句不返回

标签 c++ if-statement switch-statement

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时,它会输出going to command line B和其他两行然后返回主菜单,也就是'a',为什么不返回主菜单如果命令字符等于“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;
}
}

关于C++ if语句不返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28227919/

相关文章:

python - “损坏的双链表”内存错误 - OpenCV-Python 3.0.0 和 ARM 上的 Numpy

c++ - std::sort 和 std::unique 结构问题

javascript - 使用 if 语句和 jquery 来限制动画

R - 使用带有逻辑值的开关

c++ - boost any library 的典型用法是什么?

c# - 调用 C++/CLI 包装器时出现外部异常 E0434352

javascript - 将嵌套的 if 语句转换为更优雅的 switch?

c# - 多个条件表达式和 ": expected"错误

c++ - 在 switch 语句中使用常量数组的元素作为 case

c - 使用 argv 和 argc 打印