(如何)我可以使用'或'在switch语句中?

时间:2015-06-04 09:37:31

标签: javascript switch-statement

以下代码是否正确:

[self.view endEditing:YES];

我试了一下。但它并没有按照我的意愿行事。如果输入是“没有”,那么它运作良好。或者说,是的,但是所有后来的字符串都被忽略了!

7 个答案:

答案 0 :(得分:6)

我会使用包含您的答案的字典(对象)来处理此问题,然后您可以使用switch语句中的函数进行检查。这比多个案例检查要简洁得多:

var user = prompt("Is programming awesome?").toUpperCase(); // 'HELL YEAH';

// 'dictionary' is just a JS object that has key/value pairs
// In this instance we have keys that map the case checks you were
// making and each has a corresponding array for its value, filled with
// the OR checks you wanted to make.
// We pass 'dict' in as the second parameter when we call checkAnswer
var dict = {
    yes: ['HELL YEAH', 'YES', 'YEAH', 'YEP', 'YEA'],
    maybe: ['KINDA', 'CANT SAY'],
    no: ['HELL NO', 'NO', 'NAH', 'NOPE', 'NA']
};

// checkAnswer returns 'yes' for the user prompt 'HELL YEAH'
// It loops over the 'dict' object and if it finds an instance of
// your user input in one of the arrays it returns the key
// in this instance 'yes' for 'HELL YEAH' (indexOf returns 0)
// otherwise it returns false (for your 'default' case)
function checkAnswer(user, dict) {
    for (var p in dict) {
        if (dict[p].indexOf(user) > -1) return p;
    }
    return false;
}

// All we do is use checkAnswer to return the key where
// the user input is found in one of the dictionary arrays.
// That will always be a single string which is what switch expects
switch (checkAnswer(user, dict)) {
    case 'yes':
        console.log("That's what I'd expected!");
        break;
    case 'maybe':
        console.log("Oh, you'd like it when you'll be a pro!");
        break;
    case 'no':
        console.log("You can't say that!");
        break;
    default:
        console.log("Wacha tryna say mate?");
        break;
}

DEMO

答案 1 :(得分:3)

我想你必须这样做:

var user = prompt("Is programming awesome?").toUpperCase();
  switch (user) {
    case 'HELL YEAH':
    case 'YES':
    case 'YEAH':
    case 'YEP':
    case 'YEA':
      console.log("That's what I'd expected!");
      break;
   case 'KINDA':
      ...
}

等等......

答案 2 :(得分:1)

Javascript开关具有直通行为,您可以利用它来获得您所描述的内容:

var user = prompt("Is programming awesome?").toUpperCase();
switch (user) {
    case 'HELL YEAH':
    case 'YES':
    case 'YEAH':
    case 'YEP'
    case 'YEA':
        console.log("That's what I'd expected!");
        break;
    case 'KINDA':
    case 'CANT SAY':
        console.log("Oh, you'd like it when you'll be a pro!");
        break;
    case 'HELL NO':
    case 'NO':
    case 'NAH':
    case 'NOPE':
    case 'NA':
        console.log("You can't say that!");
        break;
    default:
        console.log("Wacha tryna say mate?");
        break;
}

不喜欢使用运算符,但它有效。 随着时间的推移,特定case的执行将跳过同一组中不匹配的那些,直到达到一些实际代码。

答案 3 :(得分:1)

不,但在C ++,Java等中,您可以执行以下操作:

switch (a_variable) {
   case CONST_1:
   case CONST_2:
   case CONST_3:
       do_something();
       break;
   case CONST_4:
       do_some_stuff();
       break;
   dafeult:
       do_default_stuff();
       break;
}

这就像or的{​​{1}}一样。这应该在JS中工作^^

答案 4 :(得分:0)

没有休息的小组案件。

var user = prompt("Is programming awesome?").toUpperCase();
    switch (user) {
        case 'HELL YEAH':
        case 'YES':
        case 'YEAH':
        case 'YEP':
        case 'YEA':
            console.log("That's what I'd expected!");
        break;
        case 'KINDA':
        case 'CANT SAY':
            console.log("Oh, you'd like it when you'll be a pro!");
        break;
        case 'HELL NO':
        case 'NO':
        case 'NAH':
        case 'NOPE':
        case 'NA':
            console.log("You can't say that!");
        break;
        default:
            console.log("Wacha tryna say mate?");
        break;
    }

答案 5 :(得分:0)

我认为开关必须将它的情况与String匹配,所以||运营商不会工作。

但是,您可以通过省略break;来定义相同结果的多个案例。

switch(word) {
  case "yes":
  case "yeah":
    goThroughWithIt();
    break;

  case "no":
  case "nuh uh":
    abort();
    break;
}

答案 6 :(得分:0)

我认为您应该修改如下:

var user = prompt("Is programming awesome?").toUpperCase();
switch (user) {
    case 'HELL YEAH':
    case 'YES': 
    case 'YEAH':
    case 'YEP': 
    case 'YEA':
      console.log("That's what I'd expected!");
    break;

    case 'KINDA':
    case 'CANT SAY':
        console.log("Oh, you'd like it when you'll be a pro!");
    break;

    case 'HELL NO':
    case'NO':
    case'NAH':
    case'NOPE':
    case'NA':
        console.log("You can't say that!");
    break;

    default:
        console.log("Wacha tryna say mate?");
    break;
}