【问题标题】:case user.length < 4; console.log("nametoo short :("); break; Why doesn't this work?case user.length < 4; console.log("nametoo short :("); break; 为什么这不起作用?
【发布时间】:2013-03-07 14:57:58
【问题描述】:

我正在处理 switch 语句,我一直在尝试使这里的代码正常工作,但它似乎没有输出正确的 console.log 大小写字符串。

var user = prompt("What is your name?").toLowerCase();
switch(user){
    case "luka":
    console.log("What a beautiful name, my name is exactly the same :) Hi Luka! :D");
    break;

    case user.length > 10:
    console.log("That's a long name!");
    break;

    case user.length < 4:
    console.log("Not to be rude or impolite to you in any way, but your name is kinda       short :( Not that it isn't cool or something :D");
    break;

}

我试过像这样 (user).length

【问题讨论】:

  • 为什么要使用这样的 switch 语句?

标签: javascript switch-statement string-length


【解决方案1】:

你不应该在 switch 语句中使用条件。

使用 if/else if

var user = prompt("What is your name?").toLowerCase();
if (user==="luka") {
    console.log("What a beautiful name, my name is exactly the same :) Hi Luka! :D");
} else if (user.length > 10) {
    console.log("That's a long name!");
} else if (user.length < 4) {
    console.log("Not to be rude or impolite to you in any way, but your name is kinda       short :( Not that it isn't cool or something :D");
} else {
    console.log("in else");
}

【讨论】:

【解决方案2】:

在像您这样的情况下使用switch 有一种可能的解决方法:

var user = prompt("What is your name?").toLowerCase();
switch (true) {
    case (user === "luka"):
        console.log("What a beautiful name, my name is exactly the same :) Hi Luka! :D");
        break;

    case (user.length > 10):
        console.log("That's a long name!");
        break;

    case (user.length < 4):
        console.log("Not to be rude or impolite to you in any way, but your name is kinda       short :( Not that it isn't cool or something :D");
}

但是我会遵循 @epascarello's 的建议并使用 if/else 块。

【讨论】:

  • 这确实有效,但它的代码比if - else 序列要多:-)
  • 绝对如此。这就是为什么没有人使用这种结构:)
  • 这绝对有效,但我很想知道为什么我的做事方式不起作用?为什么我必须用 true 替换 user 而不是将 user 变量保留在 switch 语句的括号内?你可能知道为什么吗? :S
  • 我正在学习、尝试、尝试。对 JavaScript 来说还是很新,我喜欢以不同的方式测试各种东西,所以如果有人不使用这种结构,对我来说并不重要。
  • @user2097217Pointy 给出了最佳答案。您可以将他的答案中的描述与我的代码进行比较,您将完全理解这一点。
【解决方案3】:

这不是 JavaScript switch 语句的工作方式。 “case”表达式中的值与switch 表达式的值进行比较。

你那里的语句相当于:

if (user === "luka") {
    console.log("What a beautiful name, my name is exactly the same :) Hi Luka! :D");
}
else if (user === (user.length > 10)) {
    console.log("That's a long name!");
}
else if (user === (user.length < 4)) {
    console.log("Not to be rude or impolite to you in any way, but your name is kinda       short :( Not that it isn't cool or something :D");    
}

因此,您将“用户”的值与将user.length 与这些值进行比较的结果进行比较。这些比较结果是布尔值,因此“使用”对他们来说永远不会是===

【讨论】:

  • @user2097217 你必须使用if;这不是switch 语句的工作方式。请参阅我的扩展说明。
  • 但同样,如果 user === user.length > 10 则变为 true 应该是 console.log("Thestringhere");
  • @user2097217 此答案中的代码不是解决方案。它准确地向您展示了您的代码中发生了什么。
  • 不,这相当于user === (true)user === (false),它们永远不会是true,因为字符串永远不会是布尔值的===
  • 我不明白... else if (user === (user.length > 10)) { console.log("名字好长!");对我来说,这意味着如果 user.length > 10 为真,那么 user === true 就会导致 console.log 动作。这不对吗?为什么?
猜你喜欢
  • 2013-07-14
  • 2022-01-08
  • 2018-09-26
  • 1970-01-01
  • 1970-01-01
  • 2017-12-15
  • 2011-06-13
  • 2021-12-13
  • 1970-01-01
相关资源
最近更新 更多