Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 具有多个值的switch语句_Javascript_Switch Statement - Fatal编程技术网

Javascript 具有多个值的switch语句

Javascript 具有多个值的switch语句,javascript,switch-statement,Javascript,Switch Statement,我想知道是否有一种方法可以计算开关表达式中的几个值。例如,我只想在X和Y匹配的情况下应用case。 这是我的密码: 开关x,y{ 案例x>=0&&x0&&x您需要一个if语句 if(x >= 0 && x < 150 && y == 150) topLeftRight(); else if(x == 150 && y <= 150 && y > 0) topRightDown(); else if(y

我想知道是否有一种方法可以计算开关表达式中的几个值。例如,我只想在X和Y匹配的情况下应用case。 这是我的密码:

开关x,y{ 案例x>=0&&x<150&&y==150: 左上右; 打破 案例x==150&&y 0: 自上而下; 打破
案例y===0&&x>0&&x您需要一个if语句

if(x >= 0 && x < 150 && y == 150)
  topLeftRight();
else if(x == 150 && y <= 150 && y > 0)
  topRightDown();
else if(y === 0 && x > 0 && x <= 150)
  bottomRightLeft();
else if(x === 0 && y <= 150 && y >= 0)
  bottomLeftUp();

在计算条件时,最好使用if/else if/else语句

您需要if语句

if(x >= 0 && x < 150 && y == 150)
  topLeftRight();
else if(x == 150 && y <= 150 && y > 0)
  topRightDown();
else if(y === 0 && x > 0 && x <= 150)
  bottomRightLeft();
else if(x === 0 && y <= 150 && y >= 0)
  bottomLeftUp();
在计算条件时,最好使用if/else if/else语句

您可以将true作为表达式来检查大小写。使用表达式部分和大小写部分的严格比较===

if(x >= 0 && x < 150 && y == 150)
  topLeftRight();
else if(x == 150 && y <= 150 && y > 0)
  topRightDown();
else if(y === 0 && x > 0 && x <= 150)
  bottomRightLeft();
else if(x === 0 && y <= 150 && y >= 0)
  bottomLeftUp();
switch (true) {
  case x >= 0 && x < 150 && y == 150:
    topLeftRight();
    break;
  case x == 150 && y <= 150 && y > 0:
    topRightDown();
    break;
  case y === 0 && x > 0 && x <= 150:
    bottomRightLeft();
    break;
  case x === 0 && y <= 150 && y >= 0:
    bottomLeftUp();
    break;
}
您可以将true作为表达式来检查大小写。使用表达式部分和大小写部分的严格比较===

switch (true) {
  case x >= 0 && x < 150 && y == 150:
    topLeftRight();
    break;
  case x == 150 && y <= 150 && y > 0:
    topRightDown();
    break;
  case y === 0 && x > 0 && x <= 150:
    bottomRightLeft();
    break;
  case x === 0 && y <= 150 && y >= 0:
    bottomLeftUp();
    break;
}

如果开关不完全匹配,也可以使用开关。如果开关不完全匹配,则使用if/else。如果开关也可以使用,则使用if/else没有意义。如果开关不完全匹配,则使用if/else