4

我刚开始学习 JavaScript,并且对 C# 有非常基本的了解。

尝试运行这段代码:

var number = parseInt(prompt("Enter a number:"));

switch (number) {
  case number >= 1 && number <= 9:
    alert("The number " + number + " is a single digit number.");
    break;
  case number >= 10 && number <= 99:
    alert("The number " + number + " is a two digit number.");
    break;
  case number >= 100 && number <= 999:
    alert("The number " + number + " is a three digit number.");
    break;
  case number >= 1000 && number <= 9999:
    alert("The number " + number + " is a four digit number.");
    break;
  default:
    alert("Your number has 5 or more digits.");
}

但在以下情况后它一直失败:

number <= 9

你们能帮我一下吗?

非常感谢!

4

4 回答 4

7

switch 语句确实将其参数与案例中表达式的值进行比较。

在您的代码中,它确实将number与案例中的那些布尔结果进行比较,因此它仅在您number为 1 (因为1 == true)并落在default其他情况下时才出现。您可以简单地将它们与 进行比较true,例如

switch (true) {
  case number >= 1 && number <= 9:
    alert("The number " + number + " is a single digit number.");
    break;
  case number >= 10 && number <= 99:
    alert("The number " + number + " is a two digit number.");
    break;
  case number >= 100 && number <= 999:
    alert("The number " + number + " is a three digit number.");
    break;
  case number >= 1000 && number <= 9999:
    alert("The number " + number + " is a four digit number.");
    break;
  default:
    alert("Your number has 5 or more digits.");
}

但使用if-else构造会更干净:

if (number >= 1 && number <= 9)
  alert("The number " + number + " is a single digit number.");
else if (number >= 10 && number <= 99)
  alert("The number " + number + " is a two digit number.");
else if (number >= 100 && number <= 999)
  alert("The number " + number + " is a three digit number.");
else if (number >= 1000 && number <= 9999)
  alert("The number " + number + " is a four digit number.");
else 
  alert("Your number has 5 or more digits.");

顺便说一句,要短得多

var l = String(number).length;
alert(l<5
   ? "The number "+number+" is a "+[,"single","two","three","four"][number]+" digit number."
   : "Your number has 5 or more digits."
);
于 2013-07-28T17:21:57.220 回答
1

你需要 switch(true) ,其余的保持不变(我猜)。

于 2013-07-28T17:21:39.553 回答
1

到目前为止,所有其他答案都缺少一个事实,即您不需要所有这些条件。

if (number >= 10000) {
    ...
} else if (number >= 1000) {
    ...
} else if (number >= 100) {
    ...
} else if (number >= 10) {
    ...
} else if (number >= 1) {
    ...
} else {
    ...
}

当然,还有其他方法。对于这个特定的例子,一个简单的方法是使用

var digits = (number >= 0) ? number.toString().length : 0;

对于正整数,它将是位数,对于负数,它只是0. 在这个数字上的 switch/case 语句是可能的。

Aswitch(true)是可怕的风格。它根本不是开关/机箱的用途。

于 2013-07-28T17:38:54.650 回答
0

使用 if/else 这样做:

if( number >= 1 && number <= 9)
    alert("The number " + number + " is a single digit number.");
else if( number >= 10 && number <= 99)
    alert("The number " + number + " is a two digit number.");
else if( number >= 100 && number <= 999)
     alert("The number " + number + " is a three digit number.");
else if( number >= 1000 && number <= 9999)
     alert("The number " + number + " is a four digit number.");
else
     alert("Your number has 5 or more digits.");

或者尝试使用switch(true)as 条件与布尔表达式进行比较。

于 2013-07-28T17:22:37.500 回答