【发布时间】:2015-12-08 16:47:27
【问题描述】:
如果我想把这段代码写成一行怎么办?
if (count >= 0 && count <= 199) {
return 1;
} else if (count >= 200 && count <= 399) {
return 2;
} else if (count >= 400 && count <= 599) {
return 3;
} else if (count >= 600 && count <= 799) {
return 4;
} else {
return 5;
}
我只是想知道这几行代码有什么捷径。
【问题讨论】:
-
我建议使用 switch 语句来消除多个 if/else。
-
字面意思是
return 1值 1?? -
@devlincarnate:真的吗?怎么样??
-
@KarolyHorvath - Google switch 语句?例如:tutorialspoint.com/cplusplus/cpp_switch_statement.htm
-
@devlincarnate:如果您没有理解我评论的重点,请尝试使用开关编写此代码。我是认真的。
标签: c++ if-statement