vb程序改错 求解一元二次方程(ax^2+bx+c=0)的程序
求解一元二次方程(ax^2+bx+c=0)的程序PrivateSubCommand1_Click()DimaAsInteger,bAsInteger,cAsInteger...
求解一元二次方程(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
,应该改为:
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)
"x1=";
x1,
"x2=";
x2
End
Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询