Java Script,需要帮助找到我的错误

时间:2016-04-19 22:26:34

标签: javascript if-statement

<script type="text/javascript">
 var score = prompt("Enter your score. Grade must be between 100-0.");

 if ( score>==90 ) 
    {
        document.write ( "Your grade is an A." )
    }
else if ( score <89>==80 )
    {
        document.write ( "Your grade is a B." )
    }
else if ( score <79>==70 )
    {
        document.write ( "Your grade is a C.")
    }
else if ( score <69>==60 ) 
    {
        document.write ( "Your grade is a D.")
    }
else if ( score <59>==0 )
    {
        document.write ( "Your grade is a F")
   }

}//end if
</script>

3 个答案:

答案 0 :(得分:0)

这是一个Java 脚本问题,但您需要了解“和”运算符。你真的希望每个“别人”都像:

else if ( score <= 89 && score >= 80 )

表示所有“else if”语句。如果你只有第二部分,即

,那真的可以简化
else if ( score >= 80 )

因为您之前已经检查了90以上。

编辑 - 我被编辑了 - 你不能使用&lt; ==或&gt; ==运算符。

答案 1 :(得分:0)

我不确定你在寻找什么,因为你没有提出问题,但我猜是这样的:

<script type="text/javascript">
 var score = prompt("Enter your score. Grade must be between 100-0.");

    if ( score >== 90 ) 
    {
        document.write ( "Your grade is an A." )
    }
    else if ( score >== 80 )
    {
        document.write ( "Your grade is a B." )
    }
    else if ( score >==70 )
    {
        document.write ( "Your grade is a C.")
    }
    else if ( score >==60 ) 
    {
        document.write ( "Your grade is a D.")
    }
    else if ( score >== 0 )
    {
        document.write ( "Your grade is a F")
    }

}//end if
</script>

您可能希望阅读if statements in javascript。通常你会将一个评估为真或假的条件,例如thing a&gt; == thing b

答案 2 :(得分:-1)

应该是

if ( score>==90 ) 
 {
    document.write ( "Your grade is an A." )
 }
else if ( score >==80 )
{
    document.write ( "Your grade is a B." )
} 
else if ( score >==70 )
{
    document.write ( "Your grade is a C.")
}
else if ( score >==60 ) 
 {
    document.write ( "Your grade is a D.")
 }
else if ( score >==0 )
{
    document.write ( "Your grade is a F")
 }