Javascript else-if语句错误

Javascript else if statement error

本文关键字:错误 语句 else-if Javascript      更新时间:2024-07-06

我必须制作一个满足三角形不等式定理的javascript代码,两个较小的边加起来比最大的边大。我必须使用javascript并使用提示用户输入三个数字。我不能要求用户输入我必须通过代码找到的最大数字。下面是我到目前为止得到的结果,但我在第一个else-if语句中一直出现错误,所以我不会运行。知道我的代码出了什么问题吗?

<script type="text/javascript">
        <!--

            var a = prompt("Enter the first side", "0");
                a = Number(a);
            var b = prompt("Enter the second side", "0");
                b = Number(b);
            var c = prompt("Enter the third side", "0");
                c = Number(c);
            if(a>=b, a>=c){
                if (b+c>a) {
                document.write("These numbers " + a + ", " + b + ", and " + c + " do satisfy the triangle inequality.1" );
                }
                else {
                document.write("These numbers " + a + ", " + b + ", and " + c + " do not satisfy the triangle inequality.1" );
                }
            else if(b>=c, b>=a) {
                if (c+a>b) {
                document.write("These numbers " + a + ", " + b + ", and " + c + " do satisfy the triangle inequality.2" );
                }
                else {
                document.write("These numbers " + a + ", " + b + ", and " + c + " do not satisfy the triangle inequality.2" );
                }
            }
            else {
                if (a+b>c) {
                document.write("These numbers " + a + ", " + b + ", and " + c + " do satisfy the triangle inequality.3" );
                }
                else {
                document.write("These numbers " + a + ", " + b + ", and " + c + " do not satisfy the triangle inequality.3" );
                }
            }
            }


        // -->
    </script>

if语句测试单个条件,而不是多个条件。假设您想知道两个a都大于或等于b并且a大于或等于c,则需要使用&&运算符:

if(a>=b && a>=c)

如果是OR,则是||运算符:

if(a>=b || a>=c)

没有这样的东西

if(a>=b, a>=c){

如果你想检查两者是否为真,请使用:

if (a >= b && a >= c)

如果您想检查或:

if (a > = || a >= c)

但总的来说,这个代码看起来非常糟糕。

if ((a >= b) && (a >= c))

在谷歌上搜索"javascript if"或类似的内容来了解更多信息。

我还注意到你的问题与你的答案非常不同。你说你必须求两边,然后计算第三方,但在你的回答中,你问了所有三方,然后通知用户他们是否正确。。。

是否必须使用if语句。

var input2 = [10, 30, 50, 150];
input2.sort(function(a, b){return b-a});
console.log(input2[0]);

你可以在firefox的firebug控制台中运行这个,看看它能做什么。更正了我的答案。

谢谢大家,我知道我的代码很草率,但我才刚刚开始,所以我会及时变得更好。这是我修复代码的方法。

 <script type="text/javascript">
        <!--
            var a = prompt("Enter the first side", "0");
                a = Number(a);
            var b = prompt("Enter the second side", "0");
                b = Number(b);
            var c = prompt("Enter the third side", "0");
                c = Number(c);
            if(a>=b && a>=c && b+c>a){ 
                document.write("These numbers " + a + ", " + b + ", and " + c + " do satisfy the triangle inequality." );
            } else if(b>=c && b>=a && c+a>b) {
                document.write("These numbers " + a + ", " + b + ", and " + c + " do satisfy the triangle inequality." );
            } else if(c>=a && c>=b && a+b>c) {
                document.write("These numbers " + a + ", " + b + ", and " + c + " do satisfy the triangle inequality." );
            } else {
                document.write("These numbers " + a + ", " + b + ", and " + c + " do not satisfy the triangle inequality." );
            }
        // -->
    </script>