我刚开始使用 C++,但对其他语言有一些先验知识(不幸的是,vb 不久前),但有一个奇怪的困境。我不喜欢使用这么多 IF 语句,并想使用 switch/cases,因为它看起来更干净,我想参与实践.. 但是..
假设我有以下场景(理论代码):
while(1) {
//Loop can be conditional or 1, I use it alot, for example in my game
char something;
std::cout << "Enter something\n -->";
std::cin >> something;
//Switch to read "something"
switch(something) {
case 'a':
cout << "You entered A, which is correct";
break;
case 'b':
cout << "...";
break;
}
}
这就是我的问题。假设我想退出 WHILE 循环,它需要两个 break 语句?
这显然看起来是错误的:
case 'a':
cout << "You entered A, which is correct";
break;
break;
那么我只能在'a'上做一个IF语句来使用break;吗?我错过了一些非常简单的东西吗?
这将解决我现在遇到的很多问题。