使用VB编程算平方根
2个回答
展开全部
有现成函数sqr(100)返回10
如果是任意次开方可以自己做一个函数,先做一个连乘的乘方函数,再做一个开任意次方。因为开方就是不断的用连乘来试验的,如下
'定义连乘函数,或者叫乘方Private Function lc(number, n As Integer)Dim myresultmyresult = 1Dim i As IntegerFor i = 1 To n myresult = myresult * numberNextlc = myresultEnd Function
12345678910111213141516171819202122 '开任意次方Private Function Power(number As Double, n As Integer)Dim j As DoubleDim myresult As Doublemyresult = 0.0000001For j = p To number Step 0.00001 Dim myNum As Double myNum = lc(j, n) If myNum <= number Then myresult = j Else Exit For End IfNextPower = myresultIf lc(Int(Power) + 1, n) = number ThenPower = Int(Power) + 1ElsePower = myresultEnd IfEnd Function
调用示范:
MsgBox Power(28, 3)
展开全部
Dim i As Integer
i=1
Do While i*i<=100
Dim tmp As Integer
tmp=i*i
If tmp Mod 9=0 Then
Text1.Text=Text1.Text & tmp & vbNewLine
End If
i=i+1
Loop
追问
照理说,能够被9整除的不是应该有9 18 27 36 45 54 63 72 81吗?怎么永远只显示2个数?
追答
如果只要显示能被9整除的数,如下:
标准搜索做法:
Dim i As Integer
For i=1 To 100
If i Mod 9=0 Then
Text1.Text=Text1.Text & tmp & vbNewLine
End If
Next
直接打印9的倍数:
Dim i as Integer
i=9
Do While i<=100
Text1.Text=Text1.Text & i & vbNewLine
i=i+9
Loop
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询