怎麼在VB中求和
你的問題不夠具體…那麼我給你一個例子,你參考下:
Private Sub Form_Load()
Dim a(1 To 50)
Show
For i=1 To 50
a(i)=101*RND
sum=sum+a(i)
Next i
Print sum
End Sub
以上希望能給你幫助,求和主要是用到循環,可以用For和Do。
vb求和語言
Private Sub Command1_Click()
a = Val(Text1)
s = Len(a)
Sum = 0
For i = 1 To s
Sum = Sum + a Mod 10
a = a \ 10
Next i
Text2 = Str(Sum)
End Sub
Private Sub Command2_Click()
Text1 = ""
Text1.SetFocus
Text2 = ""
End Sub
Private Sub Command3_Click()
End
End Sub
VB求和的函數是什麼
把??text3.text=text1.text+text2.text?這句改成下麵這句就可以了,
val是提取text中的數字,這樣才可以加減運算,隻有變成數字,才能加減運算。
text3.text=val(text1.text)+?val(text2.text)
如圖下圖,不懂可以追問我
VB正確幾種求和方法
答案選擇B
A
sum=0
for i=1 to 10
sum=sum+1
next i
print sum
每次都是sum+1,循環10次後sum的值是10,沒有進行1+2+3。。d的運算
B
sum=0
i=1
do while i<>
sum=sum+i
i=i+1
loop
print sum
第一次sum=0+1
第二次sum=sum+2,因為i=i+1增加了
.
C
sum=sum+1做的是sum=sum+1,相當於每次循環1+1+1+1的了,不是1+2+3+4
D
do
sum=sum+i
i=i+1
loop until i<>
這裏的條件是loop until,不是while
當初i<>
vb怎樣編寫求和∑程序?
private function msum(a as integer,b as integer)
s=0
for k=a to b
s=s+f(k)
next
msum=s
end function
private function f(k as integer)
... '提問未給出f(k)的具體算式,這裏以省略號代替
end function
用 vb 做 100以內質數求和 的程序
在窗體上添加個按鈕
Private Sub Command1_Click()
Dim i As Integer, s As Integer, j As Integer, l As Integer
For i = 2 To 100
l = 0
For j = 2 To i - 1
If i Mod j = 0 Then l = 1
Next j
If l <> 1 Then s = s + i
Next i
Print "100以內質數和為" & s
End Sub
斐波那契數列如何用VB求和
斐波那契數列的發明者,是意大利數學家列昂納多·斐波那契.
斐波那契數列指的是這樣一個數列:1、1、2、3、5、8、13、21、……
下麵是前1000項斐波那契數列的和的VB代碼:
Private Sub Command1_Click()
Dim a As Integer
Dim b As Integer
Dim c As Integer
Dim S As Long
Dim i As Integer
a = 1
b = 1
S = 2
For i = 3 To 1000
c = a + b
S = S + c
a = b
b = c
Next i
Print S
End Sub
如何用VB求和
中間的算法如下:
Dim sum As Integer
n = 99 '你輸入的最大數
i = 1
sum = 0
Do Until i > n
sum = sum + i
i = i + 1
Loop
'求和到此為止,下麵的是求素數
k = 1
Do Until k > n
For j = 1 To k / 2
If k Mod j = 0 And j <> 1 Then
Exit For
End If
Next j
If j > k / 2 And k <> 2 Then
MsgBox k
End If
k = k + 1
Loop
VB程序求和
程序本身有2處錯誤:
1.變量 s 不能申明為 Integer,應為 Single
2.語句 s = ② 將改變 s 的值,導致無法得到正確結果
因此,不改變程序結構永遠得不到正確結果
完整序列為:
1/1 + 1/2 + 2/3 + 3/5 + 5/8 + 8/13 +
13/21 + 21/34 + 34/55 + 55/89
Private Sub Command1_Click()
Dim s As Single
Dim a As Integer
Dim b As Integer
Dim k As Integer
Dim J As Integer
s = 0
a = 1
b = 1
For k = 1 To 10 '第 7 項是 21/34
s = s + a / b
J = a
a = b
b = b + J
Next
MsgBox s
Print s
End Sub
怎樣用vb求和
雙擊窗體
輸入
Private Sub Form_Load()
Dim a, b As Single
For a = 1 To 100 Step 1
b = b + 1 / a
Next
MsgBox (b)
End Sub