[英]How to use if statement right (javascript)
我得到的代碼可以很好地工作,就像我想要的那樣,但是如果我想用其他方式擴展它,那是不可能的。 隨意鏈接一些好的站點以進行學習。
if(wallLeft < marioLeft && wallLeftWidth > marioLeft)
{
}
else
{
newLeft = marioLeft - 5;
mario.style.left = newLeft + "px";
}
//else if(wallLeftWidth < marioLeft && wallLeftWidth > marioLeftWidth)
{ }
else
{ //some code }
逆轉您的條件,寫if(...){}else{...}
看起來很可怕,這實際上是一種不好的做法。 此外,該命令是if
, else if
, else
,你不能使用else if
以后else
。
if( wallLeft > marioLeft || wallLeftWidth < marioLeft ) {
newLeft = marioLeft - 5;
mario.style.left = newLeft + "px";
} else if( wallLeftWidth > marioLeft || wallLeftWidth < marioLeftWidth) {
//some code
}
您的代碼是錯誤的。 嘗試這個;
if (wallLeft < marioLeft && wallLeftWidth > marioLeft) {
newLeft = marioLeft - 5;
mario.style.left = newLeft + "px";
} else if (wallLeftWidth < marioLeft && wallLeftWidth > marioLeftWidth) {
//some code
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.