简体   繁体   English

应该使用什么语法来比较 else if 语句中的多个条件?

[英]What syntax should be used to compare multiple conditions in else if statements?

I am trying to run my code to have a result of "Child" in my console.我正在尝试运行我的代码以在我的控制台中获得“儿童”的结果。 With the variable patient being 7, I still get the result of "Teenager".变量患者为 7 时,我仍然得到“青少年”的结果。 Any suggestions?有什么建议么?

var patient = 7;
if (patient >= 18) {
  ageGroup = 'Adult';
} else if (patient <= 17) {
  ageGroup = 'Teenager';
} else if (patient <= 12) {
  ageGroup = 'Child';
}
console.log(ageGroup);

The conditions are tested in order.条件按顺序进行测试。 Since both patient <= 17 and patient <= 12 are true, the first one takes precedence, so it says they're a teenager.由于patient <= 17patient <= 12都是正确的,所以第一个优先,所以它说他们是青少年。

When you have overlapping conditions, you can change the order so the more specific one is first.当您有重叠的条件时,您可以更改顺序,以便更具体的条件优先。 You can also use else instead of testing the last condition.您也可以使用else代替测试最后一个条件。

if (patient >= 18) {
  ageGroup = 'Adult';
} else if (patient <= 12) {
  ageGroup = 'Child';
} else {
  ageGroup = 'Teenager';
}

Another options is to change the conditions so they don't overlap:另一种选择是更改条件,使它们不重叠:

if (patient >= 18) {
  ageGroup = 'Adult';
} else if (patient >= 13) {
  ageGroup = 'Teenager';
} else {
  ageGroup = 'Child';
}

Rather than using mnultiple if / else conditions - consider a switch statement - that is literally what it is intended for - to take conditions - compare them and allow code choices.而不是使用多个 if / else 条件 - 考虑一个 switch 语句 - 这实际上是它的目的 - 获取条件 - 比较它们并允许代码选择。

I also converted the single js codeblock into a function so that you coudl call it from different areas and with different ages.我还将单个 js 代码块转换为 function 以便您可以从不同的区域和不同的年龄调用它。

Note that I am doing the comparison inside the switch statements and then returning true for the matching condition.with the default being that the passed in number is that of an adult.Also the new "let" is better than var since it is now scoped to the function it is in and not cluttering up the global scope.请注意,我在 switch 语句中进行比较,然后为匹配条件返回 true。默认情况下传入的数字是成人的数字。而且新的“let”比 var 更好,因为它现在是作用域的对于 function,它位于全局 scope 中并且不会弄乱。

 function checkAge(n) { let ageGroup; switch(true) { case n <= 12: ageGroup = 'Child'; break; case n <= 17: ageGroup = 'Teenager'; break; default: ageGroup = 'Adult' } return ageGroup; } console.log(checkAge(7)); // gives Child console.log(checkAge(15)); // gives Teenager console.log(checkAge(29)); // gives Adult

The programming flow defined in your IF-ELSE contains failed in logic structure: The variable with value 7 satisfy the both conditions defined in else if . IF-ELSE 中定义的编程流程包含逻辑结构失败:值为 7 的变量满足else if中定义的两个条件。 Then, based in program flow, the console outputs "Teenager" because its the first condition deflected.然后,根据程序流程,控制台输出“Teenager”,因为它的第一个条件偏转。 The code bellow contains an example to resolves this problem:下面的代码包含解决此问题的示例:

var patient = 7;
if (patient >= 18) {
  ageGroup = 'Adult';
} else if (patient > 12 && patient <= 17) {
  ageGroup = 'Teenager';
} else if (patient <= 12) {
  ageGroup = 'Child';
}
console.log(ageGroup);

thats because of your conditions have interposition so you have two ways:那是因为你的条件有干预所以你有两种方法:

you can resolve this interposition(i suggest this way):你可以解决这个插入(我建议这样):

if (patient >= 18) {
  ageGroup = 'Adult';
} else if (patient <= 17 & patient >12) {
  ageGroup = 'Teenager';
} else if (patient <= 12) {
  ageGroup = 'Child';
}

or或者

you can change if statment places(i suggest this way if you have to have this interposition):你可以改变如果陈述的地方(如果你必须有这种干预,我建议这样):

if (patient >= 18) {
  ageGroup = 'Adult';
} else if (patient <= 12) {
  ageGroup = 'Child';

} else if (patient <= 17) {
  ageGroup = 'Teenager';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM