早教吧 育儿知识 作业答案 考试题库 百科 知识分享

vb程序改错 求解一元二次方程(ax^2+bx+c=0)的程序求解一元二次方程(ax^2+bx+c=0)的程序Private Sub Command1_Click()Dim a As Integer,b As Integer,c As Integer,d As IntegerDim x1 As Single,x2 As SingleInputBox ("a=")InputBox

题目详情
vb程序改错 求解一元二次方程(ax^2+bx+c=0)的程序
求解一元二次方程(ax^2+bx+c=0)的程序
Private Sub Command1_Click()
Dim a As Integer,b As Integer,c As Integer,d As Integer
Dim x1 As Single,x2 As Single
InputBox ("a=")
InputBox ("b=")
InputBox ("c=")
d = Sqr(b * b - 4 * a * c)
x1 = (-b + d) / (2 * a)
x1 = (-b - d) / (2 * a)
Print "x1="; x1,"x2="; x2
End Sub
请问错在哪里
▼优质解答
答案和解析
两处错误,第一个,没有赋值过程 ,应该改为:
a=InputBox ("a=")
b=InputBox ("b=")
c=InputBox ("c=")
第二个:x1 = (-b - d) / (2 * a)应该是
x2 = (-b - d) / (2 * a)
另外d有开方过程 ,最好设为 double而不是 integer
最好再加个判断判别式的过程:
Private Sub Command1_Click()
Dim a As Integer,b As Integer,c As Integer,d As Double
Dim x1 As Single,x2 As Single
a = InputBox("a=")
b = InputBox("b=")
c = InputBox("c=")
d = b * b - 4 * a * c
If d < 0 Then
MsgBox "判别式小于0"
Exit Sub
End If
d = Sqr(d)
x1 = (-b + d) / (2 * a)
x2 = (-b - d) / (2 * a)
Print "x1="; x1,"x2="; x2
End Sub