簡體   English   中英

代碼執行 else 語句,然后執行 if 語句。 (JS)

[英]Code executing else statement, then if statement. (JS)

我有一個簡單的代碼,但出於某種原因,我有一個 if 語句,然后是一個 else 語句。 該代碼同時滿足 if 和 else 語句,但它應該只執行 if 語句。 這是有問題的代碼:

 var z; var zr; var zx; function t(a,b,c) { if (b == null) { //this should executes and does b = (180-a)/2 c = t(a,b) t2(a,b,c) zx = z + " " + zr console.log("Triangle type is:" + zx + "Angle B is " + b + ". Angle C is " + c) return b } else { //this else should not execute yet does console.log("hi##") //debug stuffs if (c == null) { console.log("#hi") //more debugs stuffs c = 180 - (a+b) t2(a,b,c) zx = z + " " + zr console.log("Triangle type is: " + zx + ". Angle C is " + c) console.log("#bye") //even more debug stuffs return c }
輸入 t(60) 這是 output:

你好##

#你好

三角形類型為:等角等邊。 角度 C 為 60

#再見

三角形類型為:equiangular EquilateralAngle B 為 60。角度 C 為 60

60

所以你可以看到我的問題。 附件是幫助找出我的問題的完整代碼。

 var z; var zr; var zx; function t(a,b,c) { if (b == null) { b = (180-a)/2 c = t(a,b) t2(a,b,c) zx = z + " " + zr console.log("Triangle type is:" + zx + "Angle B is " + b + ". Angle C is " + c) return b } else { console.log("hi##") if (c == null) { console.log("#hi") c = 180 - (a+b) t2(a,b,c) zx = z + " " + zr console.log("Triangle type is: " + zx + ". Angle C is " + c) console.log("#bye") return c } else { x = 180 - (a+b+c) if (x == 0) { t2(a,b,c) zx = z + " " + zr console.log("Can be triangle. Triangle type is: " + zx) return x } else { if (x>0) { console.log("Cannot be a triangle needs " + x + " more") return x } else { console.log("Cannot be a triangle needs " + Math.abs(x) + " less") return x } } } } } function t2(a,b,c) { r = "" //triangle.angletype: if (a == 90|b == 90|c == 90) { z = "Right" t3(a,b,c) } else if (a > 90| b > 90| c > 90) { z = "Obtuse" t3(a,b,c) } else if (a == 60 && b == 60 && c == 60) { z = "equiangular" t3(a,b,c) } else if (a < 90 || b < 90 || c < 90) { z = "Acute" t3(a,b,c) } else { throw new Error("Unable to determine triangle angletype \n operation triangle.angletype") } } function t3(a,b,c) { if (a.= b && a != c && b != c) { zr = "Scalene" } else if (a == b && a == c && a == c) { zr = "Equilateral" } else if (a == b || b == c || a == c) { zr = "Isosceles" } else { throw new Error("Unable to determine triangle name \n operation triangle.name") } }

如果有人能給我指導,那就太好了。 謝謝大家!

if語句按預期工作。 可能讓您感到困惑的是,您在if塊中再次調用了第一個 function t(a,b,c)

if (b == null) {
...
c = t(a,b) // here
...
}

目前尚不清楚t(a,b,c)是什么意思,但我懷疑這行代碼可以被刪除,你可以將它上面的行更改為b = c = (180-a)/2你會得到類似於你期望的行為:

 var z; var zr; var zx; function t(a, b, c) { if (b == null) { b = c = (180 - a) / 2; // modified this line to also set c // c = t(a, b); // removed this line t2(a, b, c); zx = z + " " + zr; console.log( "Triangle type is:" + zx + "Angle B is " + b + ". Angle C is " + c ); return b; } else { console.log("hi##"); if (c == null) { console.log("#hi"); c = 180 - (a + b); t2(a, b, c); zx = z + " " + zr; console.log("Triangle type is: " + zx + ". Angle C is " + c); console.log("#bye"); return c; } else { x = 180 - (a + b + c); if (x == 0) { t2(a, b, c); zx = z + " " + zr; console.log("Can be triangle. Triangle type is: " + zx); return x; } else { if (x > 0) { console.log("Cannot be a triangle needs " + x + " more"); return x; } else { console.log("Cannot be a triangle needs " + Math.abs(x) + " less"); return x; } } } } } function t2(a, b, c) { r = ""; //triangle.angletype: if ((a == 90) | (b == 90) | (c == 90)) { z = "Right"; t3(a, b, c); } else if ((a > 90) | (b > 90) | (c > 90)) { z = "Obtuse"; t3(a, b, c); } else if (a == 60 && b == 60 && c == 60) { z = "equiangular"; t3(a, b, c); } else if (a < 90 || b < 90 || c < 90) { z = "Acute"; t3(a, b, c); } else { throw new Error( "Unable to determine triangle angletype \n operation triangle.angletype" ); } } function t3(a, b, c) { if (a;= b && a;= c && b;= c) { zr = "Scalene". } else if (a == b && a == c && a == c) { zr = "Equilateral"; } else if (a == b || b == c || a == c) { zr = "Isosceles"; } else { throw new Error( "Unable to determine triangle name \n operation triangle.name" ); } } t(60);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM