<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
一个一元二次方程为ax²+bx+c=0,请输入a、b和c的值,并计算方程的根。<br />
<input type="number" id="a" value="" placeholder="请输入a的值"/><br />
<input type="number" id="b" value="" placeholder="请输入b的值"/><br />
<input type="number" id="c" value="" placeholder="请输入c的值"/><br />
<input type="button" value="提交" onclick="jisuan()"/>
<body>
</body>
</html>
<script>
function jisuan()
{
var a1 = document.getElementById("a").value;
var b1 = document.getElementById("b").value;
var c1 = document.getElementById("c").value;
if(a1!=""&&b1!=""&&c1!="")
{
if(a1==0)
{
alert("a的值不能为0,请重新输入")
}
else
{
var d =b1*b1-4*a1*c1;
if(d<0)
{
alert("该方程没有实数根");
document.getElementById("a").value=document.getElementById("b").value=document.getElementById("c").value=""
}
else if(d==0)
{
var x =(Math.sqrt(d)-b1)/(2*a1);
alert("该方程有且只有一个实数根,根为:"+x);
document.getElementById("a").value=document.getElementById("b").value=document.getElementById("c").value=""
}
else
{
var x1 =(Math.sqrt(d)-b1)/(2*a1);
var x2 =(-Math.sqrt(d)-b1)/(2*a1);
alert("该方程有2个实数根,分别为:x1="+x1+" x2="+x2);
document.getElementById("a").value=document.getElementById("b").value=document.getElementById("c").value=""
}
}
}
}
</script>