vb程序改错 求解一元二次方程(ax^2+bx+c=0)的程序

vb程序改错 求解一元二次方程(ax^2+bx+c=0)的程序,第1张

两处错误,第一个,没有赋值过程

,应该改为:

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

Option Explicit

Private Sub Form_Click()

Dim oct_num As String

Dim length As Integer, s As Integer, n As Integer, i As Integer, d As String, j As String

oct_num = InputBox("请输入一八进制数")

'**********FOUND**********

length = len(oct_num)

s = 0

n = 0

'**********FOUND**********

For i = length To 1 Step -1

d = Mid(oct_num, i, 1)

s = s + d * 8 ^ n

n = n + 1

'**********FOUND**********

Next

Print "十进制数是:"s

End Sub

代码不长,但是错误有好几个,有的隐藏的很深,也是很容易犯的错误,我把我发现的几个给你指出来 吧(不保证完全找出来了,要下班没时间了),具体改你自己写下

num(1) = Int(Rnd * 90) + 10语句之前应该加上randomize()

For i = 1 To k

           If gcd(num(i), n) <>1 Then flag = False

       Next i

这个循环要执行完以后才会执行下面的if flag then,但是你之前只给了num(1),num(2)到

num(k)没有给值

你的k不是全局变量,最开始只给了k=1,因此For i = 1 To k

           If gcd(num(i), n) <>1 Then flag = False

       Next i

实际只执行一次,下面对k的改变没有对上面起作用

do loop中k最大到9,但是如果 执行完了flag还是true,则k=k+1之后k变为10,num(k)会越界,因为定义的时候数组长度为10,最大只有num(9)


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/10976536.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-12
下一篇 2023-05-12

发表评论

登录后才能评论

评论列表(0条)

保存