-2
<html>
    <head>
        <title>Greatest of three</title>
    </head>

    <body>
        <script type="text/javascript">
            var a = parseInt(prompt("Enter A"));
            var b = parseInt(prompt("Enter B"));
            var c = parseInt(prompt("Enter C"));

            if(a == b && b == c)
                document.write("A , B and C are equal! Give distinct numbers.")
            else if(a == b)
                document.write("A and B are equal! Give distinct numbers.");
            else if(b == c)
                document.write("B and C are equal! Give distinct numbers.");
            else if(c == a)
                document.write("A and C are equal! Give distinct numbers.");
            else if(a > b) {
                if(a > c)
                    document.write("A is the greatest");
                else
                    document.write("B is the greatest");
            }
            else {
                if(b > c)
                    document.write("B is the greatest");
                else
                    document.write("C is the greatest");
            }
        </script>
    </body>
</html>

入力として何も与えられていないのに、なぜ「C が最も優れている」と言われるのですか? また、NULL が指定されたときにそれを壊さなければならない場合、どうすればよいでしょうか?

4

6 に答える 6

7

何も入力しない場合、このコード行

var a = parseInt(prompt("Enter A"));

NaNfor a(およびその他の変数) の値を返します。

どちらNaN == NaNNaN < NaNの結果にならないためtrue、すべてのifステートメントは else ブロックに解決され、最終的には が生成されますdocument.write("C is the greatest");

この使用を確認するには、次isNaN()のようにします。

if ( isNaN( a ) ) {
  // some code to handle this
}
于 2013-01-04T15:24:21.990 に答える
2

プロンプトを表示するものを何も提供しない場合。a = parseInt(prompt("Enter A"))(NaNはa = NaNNot A Number を意味します) 。その後、条件は失敗します。NaN < NaNそのまま偽ですNaN == NaN。基本的に、片側が NaN である比較は常に false になります。ロジックをたどると、「Cは最高です」と出力されます

于 2013-01-04T15:24:09.413 に答える
1
    if(b>c)
        document.write("B is the greatest");
    else 
        document.write("C is the greatest");

ここで、ロジック全体では、 をチェックしているだけですがa>b、をチェックa>cしていません。NaN値がNaNであることをチェックするには、次を使用します。isNaN()

于 2013-01-04T15:24:56.910 に答える
0

a = NaN であるため

比較 NaN == NaN は偽です!

<body>
    <script type="text/javascript">
        var a = parseInt(prompt("Enter A"));
        var b = parseInt(prompt("Enter B"));
        var c = parseInt(prompt("Enter C"));
        document.write(a);// << os NaN
        document.write(a == b); //<< Is False!!
    if(a==b && b==c)
        document.write("A , B and C are equal! Give distinct numbers.") 
    else if(a==b)
        document.write("A and B are equal! Give distinct numbers.");
    else if(b==c)
        document.write("B and C are equal! Give distinct numbers.");
    else if(c==a)
        document.write("A and C are equal! Give distinct numbers.");

    else if(a>b)
    {
        if(a>c)
            document.write("A is the greatest");
        else
            document.write("B is the greatest");
    }
    else
    {
        if(b>c)
            document.write("B is the greatest");
        else 
            document.write("C is the greatest");
    }   
    </script>
</body>
于 2013-01-04T15:26:47.037 に答える
0

三項論理では、Null を含む関係は常に false を返します。2 つの Null 値の偶数比較。Javascript の NaN 値は、次の規則に従います。

したがって、elseパスは常に使用されます。

于 2013-01-04T15:24:33.420 に答える
0

これがきっかけになるからです。

else
{
    if(b>c)
        document.write("B is the greatest");
    else 
        document.write("C is the greatest");
}   

そして、入力bがない場合は ==cなので、最終else(C が最大) がトリガーされます。

入力したことを確認する必要があります。これは、余分なブロックを全体にラップすることで実現できます。

if( a && b && c ) {
   // your if statements
}
else {
   alert("No input!")
}
于 2013-01-04T15:24:53.790 に答える