C++ if 语句不返回
C++ if statements not returning
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
函数完成执行时。
相关文章:
- 返回语句后的代码,没有警告
- 三元运算符在返回语句中给出意外的结果
- 返回非 void 的函数中没有返回语句警告
- 如何使用单个返回语句返回对变量的引用
- 当返回语句时,逗号运算符、大括号初始化列表和 std::unique_ptr 组合在一起
- 获取上次执行的返回语句的行
- 返回语句中矢量初始化时出现编译错误
- 快速搜索链表未编译,需要返回语句
- 静态成员函数赋值而不带返回语句
- 在返回语句中构造对象时,std::move() 是否有助于或阻止 RVO?
- 为什么返回语句和打印语句返回不同的答案?
- C++ 非 void 函数的崩溃没有返回语句
- 在返回语句中嵌套条件运算符
- 为什么结构化绑定禁用RVO和移动返回语句
- C - 程序返回值而无需返回语句
- 返回语句中的构造函数语法
- 编译器遇到返回语句时会做什么
- 是一个单一的返回语句清洁器
- C NRVO/复制Elision在括号中带有返回语句
- C 编译器 - 被遗忘的返回语句