在考试学习指南上,我的老师告诉我们,我们需要知道如何编写 switch 语句,但它必须排除浮点值。在我们的学习小组中,我们玩弄了使用“自动”的想法,但我们不确定如何实现他所要求的。有什么建议吗?
最佳答案
switch
statement 条件中使用的表达式必须是:
any expression of integral or enumeration type, or of a class type contextually implicitly convertible to an integral or enumeration type, or a declaration of a single non-array variable of such type with a brace-or-equals initializer.
浮点类型与其中任何一个都不匹配。
你说:
but it must exclude float values.
您无需执行任何操作来排除在 switch
语句的条件中使用浮点表达式。
如果您尝试使用,您的编译器应该报告错误:
double x;
std::cin >> x;
switch (x)
{
...
}
关于c++ - 如何从 switch 语句中排除浮点值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49182096/