gpt4 book ai didi

C++ if语句不返回

转载 作者:行者123 更新时间:2023-11-30 02:41:19 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com