回答:
你的代码中出现了几个问题:
1. 条件判断错误:你使用了常量字符比较,而不是变量字符。在你的代码中,if ('M' == 'A')、else if ('M' == 'D')和else if ('M' == 'S')这些条件判断都是错误的,因为你应该使用变量ch5来进行判断。修改为if (ch5 == 'A')、else if (ch5 == 'D')和else if (ch5 == 'S')才能正确比较输入的字符。
2. 循环控制错误:你在代码中使用了continue关键字和break关键字,但是它们只能用在循环语句中,而不能用在main()函数中。如果你想要实现循环选择菜单的功能,可以将代码放在一个循环中。
下面是修改后的代码:#include<iostream>#include<cstdlib>using namespace std;int main(){ char ch1 = 'A'; char ch2 = 'D'; char ch3 = 'S'; char ch4 = 'Q'; char ch5 = 'M'; while (true) { cout << "Menu: A(dd) D(elete) S(ort) Q(uit), Select one: "; cin >> ch5; if (ch5 == 'A') { cout << "数据已经增加" << endl; } else if (ch5 == 'D') { cout << "数据已经删除" << endl; } else if (ch5 == 'S') { cout << "数据已经排序" << endl; } else if (ch5 == 'Q') { cout << "退出程序" << endl; break; } else { cout << "无效选择,请重新输入" << endl; } cout << endl; } return 0;}
这样修改后的代码可以实现根据输入字符选择菜单功能,并且在选择 Q(uit)时退出程序。
希望能帮到你!如有需要请随时提问。
球一个最佳答案谢谢啦!这对我非常重要!   |