日期:2014-05-17 浏览次数:21064 次
<html> <head> <title>求数值的阶乘</title> <script language="javascript" type="text/javascript"> function factorial(anumber) { anumber = Math.floor(anumber); if (anumber<0) { return -1; } if (anumber==0) { return 1; } else return(anumber*factorial(anumber-1)); } </script> </head> <body> <h2>请在文本框中输入一个整数,然后单击“计算”按钮,计算该整数的阶乘。</h2> <form name="myform"> <input id="num" type="text" name="num" value=""><p> <input type="button" value="计算" onclick="result.value=factorial(num.value)"><p> <input id="result" type="text" name="result" value=""><p> </form> </body> </html>
------解决方案--------------------
给你调试了一下,现在可以了:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
<script language="javascript" type="text/javascript">
function result(){
var num=document.getElementById("factorial").value;
document.getElementById("txtOutput").value=factorial(num);
}
function factorial(anumber)
{
anumber=Math.floor(anumber);
if (anumber<0)
{
return -1;
}
if (anumber==0)
{
return 1;
}
else{
anumber=anumber*factorial(anumber-1);
}
return anumber;
}
</script>
</head>
<body>
<h2>请在文本框中输入一个整数,然后单击“计算”按钮,计算该整数的阶乘。</h2>
<form name="myform">
输入数字:<input name="factorial" value=""><p>
输出结果:<input name="txtOutput" value=""><p>
<input type="button" value="计算" onclick='result()' /><p>
</form>
</body>