这是我的代码。我正在制作一个网站,为我的需求求解二次公式。并且它需要从一个表单中调用至少三个变量
<form action="Variable.asp" id="arrays" method="get">
<label for="category">A: </label><br>
<input name="aname" id="aname"><br>
<label for="category">B: </label><br>
<input name="bname" id="bname"><br>
<label for="category">C: </label><br>
<input name="cname" id="cname"><br>
<input type="submit">
</form>
<%
Function solvex1(a,b,c)
x1 = (b * b)
x1 = x1 - (4 * (a * c))
x1 = Sqr(x1)
x1 = -b + x1
x1 = x1 / (2 * a)
x1 = CInt(x1)
x1 = x1
End Function
Function solvex2(a,b,c)
x2 = (b * b)
x2 = x2 - (4 * (a * c))
x2 = Sqr(x2)
x2 = -b - x2
x2 = x2 / (2 * a)
x2 = CInt(x2)
x2 = x2
End Function
Response.Write("Here is your answer: <br>")
Response.Write("X1 = ")
Response.Write(solvex1(Request.Form("aname"), Request.Form("bname"), Request.Form("cname")) & "<br>")
Response.Write("X2 = ")
Response.Write(solvex2(Request.Form("aname"), Request.Form("bname"), Request.Form("cname")) & "<br>")
%>
主要问题是函数调用不会返回任何内容,但希望在Response.Write()
调用中输出它们的值。
尝试更改函数定义以显示您想要返回值。这里是重写的solvex1()
函数,以返回x1
:的值
Function solvex1(a,b,c)
Dim x1
x1 = (b * b)
x1 = x1 - (4 * (a * c))
x1 = Sqr(x1)
x1 = -b + x1
x1 = x1 / (2 * a)
x1 = CInt(x1)
x1 = x1
'Return the value from the function.
solvex1 = x1
End Function
若要从函数返回值,请将其指定给函数的名称。
此外,强烈建议在执行任何数学计算之前,检查传递到函数中的值是否为有效数值。
有用的链接
- 从VBScript函数返回值
- 如何指示字符串是否具有数值