Javascript else if语句错误

时间:2015-02-27 23:12:52

标签: javascript if-statement

我必须制作一个满足三角不等式定理的javacript代码,两个较小的边加起来比最大边大。我必须使用javascript并使用提示输入三个数字的用户。我不能要求用户输入我必须通过代码找到的最大数字。所以下面是我到目前为止,但我继续得到一个错误在第一个别的如果声明,所以我不会运行。我的代码有什么问题吗?

<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>

5 个答案:

答案 0 :(得分:2)

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

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

如果是OR,那就是||运算符:

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

答案 1 :(得分:0)

没有这样的事情

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

如果要同时检查两者是否为真,请使用:

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

如果您想检查或:

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

但总的来说,这段代码看起来很糟糕。

答案 2 :(得分:0)

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

在Google上搜索“javascript if”或类似内容以了解详情。

我也注意到你的问题非常与你的答案不同。你说你必须要求双方并计算第三方,但在你的答案中你要求所有这三个,并告诉用户他们是否正确的价值......

答案 3 :(得分:0)

您是否必须使用if语句。

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

你可以在firefox的firebug控制台中运行它来查看它的功能。纠正了我的回答。

答案 4 :(得分:-1)

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

 <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>