繁体   English   中英

if else if else语句(JS)

[英]if else if else statements (JS)

我的程序有问题。 首先,我将告诉您这是什么意思:它应该为您执行勾股定理。 定理解释在这里。

这是数学公式:(a * a)+(b * b)=(c * c)

这是程序:

function pythagorean (a, b, c) {
if (c === null){
    return 'c = ' + Math.sqrt( (a*a) + (b*b) );
} else if (b === null) {
    return 'b = ' + Math.sqrt( (c*c) - (a*a) );
} else if (a === null) {
    return 'a = ' + Math.sqrt( (c*c) - (b*b) );
} else {
    return "I'm sorry, I don't understand."
};
};

我决定花哨并尝试使它如此,如果双方:A和B均为空,则程序将假定a === b,并且公式将变为: [无论C是什么] c = Math.sqrt( 2 *((a * a)+(b * b)))

(对不起,令人困惑)

因此,我尝试实现一些代码以使其工作:

function pythagorean (a, b, c) {
if (c === null){
    return 'c = ' + Math.sqrt( (a*a) + (b*b) );
 } else if (a && b === null){
    return 'if a and b are equal, then a and b both equal ' + Math.sqrt((c * c) / 2);
 } else if (b === null) {
    return 'b = ' + Math.sqrt( (c*c) - (a*a) );
 } else if (a === null) {
    return 'a = ' + Math.sqrt( (c*c) - (b*b) );
 } else {
    return "I'm sorry, I don't understand."
 };
};

但是每当我给C一个值,但a和b === null时,它都会奇怪地跳过if语句,然后下降到b === null else if语句并给我:'b ='和一些随机变量回答。 我需要你们的帮助!

条件运算符应该像这样

a ===空&& b ===空

因为它将检查a为空,b也为空。

a && b ===空

上面的条件将检查a是否具有某个值(例如1)不应该为“ 0”且为null,NaN,''和“”且b为null。

  function pythagorean (a, b, c) { if (c === null){ return 'c = ' + Math.sqrt( (a*a) + (b*b) ); } else if (a === null && b === null){ return 'if a and b are equal, then a and b both equal ' + Math.sqrt((c * c) / 2); } else if (b === null) { return 'b = ' + Math.sqrt( (c*c) - (a*a) ); } else if (a === null) { return 'a = ' + Math.sqrt( (c*c) - (b*b) ); } else { return "I'm sorry, I don't understand." }; }; var abc = pythagorean(null,null,2); alert(abc); 

if (a && b === null)不是检查它们是否均为null的正确方法,其作用是检查a是否为b是否为null ,有效地:

if ((a) && (b === null))

您应该改用类似以下的方法:

if ((a === null) && (b === null))

这是解决立即解决问题,但是,如果你正在寻找处理所有错误的情况下的解决方案(如边的一个是null当斜边也为空),你可能会更好扩展你的检查有点。 您可能会认为这会使代码复杂化,但是您也可以通过了解先前检查无法实现的情况来大大简化代码。

由于我并不是if (something) return else范式的忠实拥护者(在这种情况下else总是多余的),因此我将其删除。 我也倾向于更有意义的变量名。

以下代码合并了所有这些更改,并添加了解释每个部分的注释:

// pythagorean:
//    Returns string indicating "missing" info for a 
//    right-angled triangle (two regular sides and the
//    hypotenuse). Basic outcome is:
//        Hypotenuse null, sides given, return hypotenuse.
//        Sides null, hypotenuse given, return equal sides.
//        Hypotenuse and only one side given, return other side.
//        Anything else, error given.

function pythagorean (s1, s2, hyp) {
    // At least one value must be null.

    if ((hyp !== null) && (s1 !== null) && (s2 !== null)) {
        return "ERROR: all fields given, don't know what you want";
    }

    // If hypotenuse is null, need both other sides or error.
    // Returns hypotenuse.

    if (hyp === null) {
        if ((s1 === null) || (s2 === null)) {
            return "ERROR: hypotenuse is null, other sides cannot be";
        }
        return "hypotenuse is " + Math.sqrt((s1 * s1) + (s2 * s2));
    }

    // At this point, we KNOW we have hypotenuse, don't check again.
    // If BOTH sides are missing, assume equal.

    if ((s1 === null) && (s2 === null)) {
        return "sides are both " + Math.sqrt((hyp * hyp) / 2);
    }

    // At this point, we have hypotenuse and exactly one side
    // (not the other), so just return the other side.

    if (s1 === null) {
        return "side 1 is " + Math.sqrt((hyp * hyp) - (s2 * s2));
    }
    return "side 2 is " + Math.sqrt((hyp * hyp) - (s1 * s1));
}

这个看起来正确

 else if (a==0&b==0)

暂无
暂无

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

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