将大小写切换到/之间
Javascript 有没有一种方法可以通过 switch case 结构将一个整数与另一个整数进行比较,而不使用 if 语句?
例如
switch(integer) {
case 1 to 10:
break;
case 11 to 20:
break;
case 21 to 30:
break;
}
Is there a way in Javascript to compare one integer with another through switch case structures without using if statements?
E.g.
switch(integer) {
case 1 to 10:
break;
case 11 to 20:
break;
case 21 to 30:
break;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以进行一些数学运算。
You can do some math manipulations.
有办法,是的。我很确定我会在自己的代码中使用 if/else 结构,但如果您热衷于使用开关,则以下内容将起作用:
当然,如果您想避免编写
>= && <=
在每种情况下,您都可以定义自己的isInRange(num,min,max)
类型函数来返回布尔值,然后说:There is a way, yes. I'm pretty sure I'd use an if/else structure in my own code, but if you're keen to use a switch the following will work:
Of course if you wanted to avoid having to code
>= && <=
on every case you could define your ownisInRange(num,min,max)
type function to return a boolean and then say:正如我的评论中所述,你不能这样做。但是,您可以定义一个
inRange
函数:并将其与
if - else if
一起使用。这应该使它很容易阅读:As stated in my comment, you can't do that. However, you could define an
inRange
function:and use it together with
if - else if
. That should make it quite easy to read:发布“酷”语法:P
Posting for "cool" syntax :P