gpt4 book ai didi

switch-statement - 检查 switch 语句中的数字

转载 作者:行者123 更新时间:2023-12-03 23:38:48 27 4
gpt4 key购买 nike

是否有使用 switch 语句检查数字 0-9 的简单方法?我正在编写一个程序来检查某些字符和数字。就像检查“\0”、“F”或“f”一样,想知道是否还有一种方法可以以类似的方式检查 0-9。我知道我可以编写一个程序来在字符是数字 0-9 时返回 true 或 false,但不确定如何在 switch 语句中的一种情况下使用它。就像我有:

const int lowerBound = 48;
const int upperBound = 57;

bool isDigit(char *digit)
{
if (*digit >= lowerBound && *digit <= upperBound) {
return true;
}
else {
return false;
}
}

我怎么走

switch (*singleChar) {
case(???):
}

最佳答案

switch(mychar) {

case '0':
case '1':
case '2':
..
// your code to handle them here
break;

.. other cases
}

这称为“fall-through”——如果一个 case block 没有以 break 结束,控制流将在下一个 case 语句处继续。

关于switch-statement - 检查 switch 语句中的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2261844/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com