在该循环中(假设amount
是2时),而不是打印:C++ for循环和switch语句的逻辑错误
1. [op1]
2. [op2]
它打印:
1. [op1]
2. [op2]
3. [op3]
4. [op4]
5. [op5]
为什么呢?我怎样才能解决这个问题?
for(int i=1;i<=amount;i++){
//string s;
switch(i){
case 1:
cout << i << ". " << op1 << endl;
//s = op1;
case 2:
cout << i << ". " << op2 << endl;
//s = op2;
case 3:
cout << i << ". " << op3 << endl;
//s = op3;
case 4:
cout << i << ". " << op4 << endl;
//s = op4;
case 5:
cout << i << ". " << op5 << endl;
//s = op5;
}
//cout << i << ". " << s << endl;
}
认为开关是一个荣耀的'goto':它跳转到一个案例,只是从那里去,好像案件是一个标签和标签由'switch'表达式选择,要停止执行,在case结尾使用'break;' – GManNickG 2011-03-21 19:58:54
实际上你应该获得'[op1] [op2] [op3] [op4] [op5] [op2] 【O p3] [op4] [op5]',对吧? – Vlad 2011-03-21 19:59:43
@Vlad - 是的,这是OP应该得到的。 – 2011-03-21 20:09:53