首页 > 解决方案 > 为什么我的 if 语句没有显示在我的网站上?

问题描述

我试图弄清楚为什么我的 if 语句没有显示在我的网站上。我的第一个脚本正在显示三角形区域,我想尝试找出如何使用 ifelse 语句显示哪个三角形更大。这是我第一次尝试使用这些并希望得到一些帮助。

编辑:我已经修复了我的 if 语句的括号,但现在即使它们不正确,它也会显示它们。我应该如何解决这个问题?

编辑2:谢谢大家的惊人帮助。我很感激,我的代码现在可以工作了!

<script>
var Base1 = parseFloat(prompt("Base1", "100"));
var Height1 = parseFloat(prompt("Height1", "100"));
var Base2 = parseFloat(prompt("Base2", "100"));
var Height2 = parseFloat(prompt("Height2", "100"));

document.write("<p>triangle1's Volume is : "+Base1 * Height1/2+"</p>");
document.write("<p>triangle2's Volume is : "+Base2 * Height2/2+"</p>");
</script>
<script>
if((Base1 * Height1/2) == (Base2 * Height2/2));
{
  document.write("<p>the triangles are the same size</p>");
}
if ((Base1 * Height1/2) > (Base2 * Height2/2));
{
    document.write("<p>triangle 1 is bigger</p>");  
}
if ((Base1 * Height1/2) < (Base2 * Height2/2)); 
{
    document.write("<p>triangle2 is bigger</p>");
}
  
</script> 

标签: javascripthtml

解决方案


您的 if 条件语法不正确:

<script>
var Base1 = parseFloat(prompt("Base1", "100"));
var Height1 = parseFloat(prompt("Height1", "100"));
var Base2 = parseFloat(prompt("Base2", "100"));
var Height2 = parseFloat(prompt("Height2", "100"));

document.write("<p>triangle1's Volume is : "+Base1 * Height1/2+"</p>");
document.write("<p>triangle2's Volume is : "+Base2 * Height2/2+"</p>");
</script> 

<script>
if((Base1 * Height1/2) === (Base2 * Height2/2))
{
  document.write("<p>the triangles are the same size</p>");
} else if ((Base1 * Height1/2) > (Base2 * Height2/2))
{
    document.write("<p>triangle 1 is bigger</p>");  
}
else
{
    document.write("<p>triangle2 is bigger</p>");
}
</script>


推荐阅读