0

jsonリクエストからのいくつかの番号の処理に問題があります。結果に基づいて、HTMLのさまざまなビットを出力しようとしています。特に問題は、数値が-1より大きく6より小さいかどうかを確認するときです。コードの抜粋は次のとおりです…</ p>

else if(parseInt(myvariable, 10) < -1) {
//this is where one thing happens
}
else if(parseInt(myvariable, 10) > -1) {
//Something else happens here
}
else if(parseInt(myvariable, 10) > 6) {
//This is where the third thing should happen, but doesn't?
}

値が7または70であるにもかかわらず、2番目の「elseif」は可能な限り遠いようです。

次の条件文に進むために、数値が-1より大きく6より小さいことを確認する方法はありますか?

(前の質問のように)非常に簡単な答えがあると思いますので、私の素朴さを許してください。

前もって感謝します。

4

5 に答える 5

0

if条件が間違っています。これについて考えてみましょう。myvariableは7です。

あなたのコードで起こります:

else if(parseInt(myvariable, 10) < -1) {
//this is where one thing happens
}
else if(parseInt(myvariable, 10) > -1) {
**// HERE THE CONDITION IS TRUE, BECAUSE 7 > -1**
}
else if(parseInt(myvariable, 10) > 6) {
// This is where the third thing should happen, but doesn't?
}

次のように変更できます

else if(parseInt(myvariable, 10) < -1) {
//this is where one thing happens
}
else if(parseInt(myvariable, 10) > 6) {
// This is where the third thing should happen, but doesn't?
}
else if(parseInt(myvariable, 10) > -1) {
 // Moved
}

それを機能させるために...

于 2012-07-15T09:59:38.883 に答える
0

条件は、1つが真であることが判明するまでのみ実行されます。

つまり、現在の注文を機能させるには、注文を再調整するか、引き締める必要があります。

7は-1を超えているため、2番目の条件はtrueに解決されます。したがって、7の場合、3番目の条件は必要ありません。

if(parseInt(myvariable, 10) < -1) {
    //number is less than -1
}
else if(parseInt(myvariable, 10) > 6) {
    //number is above 6
}
else {
    //neither, so must be inbetween -1 an 6
}
于 2012-07-15T10:00:39.083 に答える
0

はい。-1より大きい数値を書き込むと、コードの3番目のブロックがスローされることはないため、「数値は-1より大きく6未満」と言ったように、2番目のブロックがスローされます。このように簡単に実行できます。 :

else if(parseInt(myvariable, 10) < -1) {
//this is where one thing happens
}
else if(parseInt(myvariable, 10) > -1 && parseInt(myvariable, 10) < 6) {
//Something else happens here
}
于 2012-07-15T10:02:15.007 に答える
0

別の解決策は、2行目を変更することです。

else if(parseInt(myvariable, 10) > -1)

に:

else if(parseInt(myvariable, 10) <= 6)

これを書く方法はたくさんあります。

于 2012-07-15T10:05:59.710 に答える
0

私はあなたがこれを簡単に次のようなことをすることができると思います:

変数値を考慮すると(7):

else if(parseInt(myVariable, 10) &lt; -1) {
//this is where one thing happens
}
else if(parseInt(myVariable, 10) > -1) {
//now 'myVariable' is greater than -1, then let's check if it is greater than 6
if(parseInt(myVariable, 10) > 6) {
//this where what you should do if 'myVariable' greater than -1 AND greater than 6
}
}
于 2012-07-15T10:18:13.273 に答える