簡體   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