在switch语句中如何使用"or"?

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

我尝试过了,但它并没有按照我的期望工作。如果输入是“hell no”或“hell yeah”,那么它可以正常工作,但之后的所有字符串都被忽略了!


快速的答案是不行。 - Andy
separate those || with cases - AkshayJ
7个回答

6
我会使用一个包含答案的字典(对象)来处理此问题,然后可以使用switch语句中的函数进行检查。这比多个case检查更加整洁:
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;
}

演示


嗯,我目前正在学习JS。所以我完全不理解你的代码!但还是谢谢你。希望有一天它会对我有所帮助! :) - Perceptioner
我会更好地注释代码以帮助你。 - Andy
@Perceptioner,我已经更新了我的答案,并提供了一些指针来帮助你以后的工作。 - Andy

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':
      ...
}

And so on...


1
Javascript的switch语句具有“落入”行为,您可以利用这一点来获得您所描述的内容:
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;
}

不像使用或运算符那样花哨,但它确实有效。通过 fall-through,执行特定的 case 将会跳过同一组中不匹配的 case,直到达到一些真正的代码。

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;
}

这就像是 switch-case 中的一个 or。在 JavaScript 中应该也可以使用^^

0

不使用 break 语句的情况下分组 case。

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;
    }

0

我相信 switch 语句需要匹配字符串类型的 case,所以 || 运算符不会起作用。

然而,你可以通过省略 break; 来定义多个 case 对应同一结果。

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

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

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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接