简体   繁体   English

在JavaScript中用switch语句编写if..else语句

[英]Write an if..else statement with a switch statement in JavaScript

How can I write this if..else statement with a switch statement in JavaScript? 如何在JavaScript中使用switch语句编写if..else语句?

var hour = new Date().getHours();
var msg = "";
if (hour >= 0 && hour < 6) {
    msg = "بامداد شما بخیر !";
} else if (hour >= 6 && hour < 11) {
    msg = "صبح شما بخیر !";
} else if (hour >= 11 && hour < 15) {
    msg = "ظهر شما بخیر !";
} else if (hour >= 15 && hour < 20) {
    msg = "عصر شما بخیر !";
} else if (hour >= 20 && hour < 24) {
    msg = "شب شما بخیر !";
} else {
    msg = "ساعت وارد شده نامعتبر است !";
}
print(hour);
print("------------------------");
print(msg);

Since hour has just 24 discrete values, you could just handle all of them in a switch-case: 由于hour只有24个离散值,因此您可以在切换情况下处理所有这些离散值:

switch (hour) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
    msg = "بامداد شما بخیر !";
    break;
case 6:
case 7:
case 8:
case 9:
case 10:
    msg = "صبح شما بخیر !";
    break;
case 11:
case 12:
case 13:
case 14:
    msg = "ظهر شما بخیر !";
    break;
case 15:
case 16:
case 17:
case 18:
case 19:
    msg = "عصر شما بخیر !";
    break;
default:
    msg = "شب شما بخیر !";
}

But honestly, isn't a wasted effort. 但老实说,这不是浪费的努力。 Using if-else is much more appropriate. 使用if-else更合适。

I'll just say at the outset that I wouldn't rewrite it as a switch . 一开始我只是说我不会将它改写成switch But I would definitely change it. 但是我肯定会改变它。 (I'll do the switch below, though.) (不过,我将在下面进行switch 。)

First, your "ساعت وارد شده نامعتبر است !" 首先,您的"ساعت وارد شده نامعتبر است !" case will never happen. 这种情况永远不会发生。 getHours returns a number between 0 and 23, inclusive. getHours返回0到23之间的数字(包括0和23)。 So there's no need to handle the case of < 0 or >= 24 . 因此,无需处理< 0>= 24

Here's how I'd write it: 这是我的写法:

var hour = new Date().getHours();
var msg;
if (hour < 6) {
    msg = "بامداد شما بخیر !";
} else if (hour < 11) {
    msg = "صبح شما بخیر !";
} else if (hour < 15) {
    msg = "ظهر شما بخیر !";
} else if (hour < 20) {
    msg = "عصر شما بخیر !";
} else {
    msg = "شب شما بخیر !";
}
print(hour);
print("------------------------");
print(msg);

Note how we're not having to write our limits in two places. 请注意,我们不必在两个地方写下限制。 There's no need for it, since we won't evaluate later else if s if the earlier if was true. 这是没有必要的,因为如果较早的if是true,我们将不会再评估else if

Now, for the actual switch , we do something similar, using the fact that JavaScript's switch is more powerful than in many similar-looking languages (JavaScript's switch is basically just an if/else if ): 现在,对于实际的switch ,我们使用JavaScript的switch比许多相似外观的语言更强大的事实(JavaScript的switch基本上只是if/else if )来做类似的if/else if

var hour = new Date().getHours();
var msg;
switch (true) {
    case hour < 6:
        msg = "بامداد شما بخیر !";
        break;
    case hour < 11:
        msg = "صبح شما بخیر !";
        break;
    case hour < 15:
        msg = "ظهر شما بخیر !";
        break;
    case hour < 20:
        msg = "عصر شما بخیر !";
        break;
    default:
        msg = "شب شما بخیر !";
        break;
}
print(hour);
print("------------------------");
print(msg);

How that works: JavaScript's switch allows the case labels to be expressions . 工作原理:JavaScript的switch允许case标签为expression It evaluates each case in source code order against the value you give in the switch , taking the first case that matches. 它根据您在switch给出的值, 按照源代码顺序评估每种case ,并采用匹配的第一种情况。 If no cases match, it uses the default (regardless of where the default is in the source code). 如果没有大小写匹配,它将使用default (无论默认值在源代码中的什么位置)。

Again: I wouldn't do that. 再说一次:我不会那样做。 But the question was how to do it. 但是问题是如何做到的。 :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM