VB编程“求1到1000以内所有奇数之和”(do while 结构)

Private Sub Command1_Click()
Dim i As Integer
Dim s As Integer
s = 0: i = 0
If i Mod 2 = 1 Then
Do While i > 1000
i = i + 1: s = s + i
Loop
End If
Print "计算1至1000之间的奇数之和为:"; s
End Sub
————————————————————————————————————
编了一个这样的得出和为0,求解问题何在?

第1个回答  2013-02-11
先把代码发出来,等会在评论中给你解释吧。复制粘贴以下代码:
Option Explicit
Private Sub Command1_Click()
Dim i As Long
Dim s As Long
s = 0
i = 0
Do While i < 1000
i = i + 1
If i Mod 2 = 1 Then
s = s + i
End If
Loop
Print "计算1至1000之间的奇数之和为:"; s
End Sub
看运行结果吧
第2个回答  推荐于2017-12-15
Private Sub Command1_Click()
Dim i As Integer
Dim s As Integer
s = 0: i = 0
Do While i <= 1000
i = i + 1:
If i Mod 2 = 1 Then
s = s + i
End If
Loop
Print "计算1至1000之间的奇数之和为:"; s
End Sub
没有学过VB但是楼主的程序确实存在问题,这样改一下应该差不多了,对于楼上说的溢出问题就要看整形数的表示范围了。本回答被提问者和网友采纳
第3个回答  2013-02-11
这个题目不需要IF语句的。S定义为Integer会溢出的。
Private Sub Command1_Click()
Dim i As Integer
Dim s As Long
s = 0: i = 1
Do While i < 1000
s = s + i: i = i + 2
Loop
Print "计算1至1000之间的奇数之和为:"; s
End Sub

VB编程“求1到1000以内所有奇数之和”(do while 结构)
Do While i <= 1000i = i + 1:If i Mod 2 = 1 Then s = s + iEnd IfLoopPrint "计算1至1000之间的奇数之和为:"; sEnd Sub没有学过VB但是楼主的程序确实存在问题,这样改一下应该差不多了,对于楼上说的溢出问题就要看整形数的表示范围了。 本回答由提问者推荐 举报| 答案纠错 | 评论 4 2 ...

vb编程,1000以内所有素数和,急用
Do While j < i If i Mod j = 0 Then f = False: Exit Do j = j + 1 Loop If f Then debug.Print i & vbTab;c = c + 1 sum=sum+i If c Mod 5 = 0 Then debug.Print End If i = i + 1 Wend print vbcrlf & "1000之内所有素数之和=" & sum End Sub ...

怎么用 VB求 1—1000之间的完数?
Private Sub Form_click()For i = 1 To 1000 k = i s = 1 j = 2 Do If k Mod j = 0 Then '判断是否能整除 s = s + j '因子相加 k = k \/ j '为下次准备 Else '实在除不断了。j = j + 1 '+1后再除 End If Loop While j < i 'If s > 1 And s ...

急需vb编程求100以内所有奇数和及所有偶数和
方法一:通过按钮“求2到100之间的偶数和”,代码如下:vb Private Sub Command1_Click()Dim i As Integer Dim s As Integer s = 0 For i = 2 To 100 Step 2 s = s + i Next i Print "2到100的所有偶数和是:" & s End Sub 方法二:通过按钮“方法2”,代码如下:vb Private Sub...

用VB的do...loop循环写语句!!!急!!!求大神!!!
10 = 0 Then x = x + i End If Next 'MsgBox "1到1000中10的倍数的和是:" & x '第三题 x = 0 Do y = y + 1 x = x + y If x > 1000 Then Exit Do Loop ' MsgBox "求恰好使s=1+2+3+…+n的值大于1000时n的值是:" & y'第四...

...如对1~100之间的整数进行求和 for及Dowhile语句实现vb
Private Sub Form_Click()Dim s, i As Integer s = 0 For i = 1 To 100 s = s + i Next i Print s End Sub Private Sub Form_Click()Dim i, s As Integer s = 0 i = 1 Do While i <= 100 s = s + i i = i + 1 Loop Print s End Sub ...

用VB中do while循环语句编写程序计算SUM=1+1*2+1*2*3+……+1*2*3*...
package number4;\/**1+1*1+1*1*2+1*1*2*3...*100; *\/ public class jiecheng { static long a,sun=1,sum=1;public static void main(String[] args) { for(a=1;a<=100;a++){ sun*=a;sum+=sun;} System.out.print(sum);} } ...

利用while,for,do-while循环语句计算求和问题:S=1-1\/2+1\/3-1\/4+...
在excel 中用vba 输入以下代码即可:(vb中也可以)Sub aaa()Dim s, i, k, m For i = 1 To 1000 If i Mod 2 = 0 Then k = 0 - i Else k = i End If s = s + 1 \/ k Next MsgBox Format(s, "0.00")End Sub

用VB编一个程序,求从1至100的自然数的和
dim i as integer dim sum as integer i = 1 方法1 do while i 100 sum = sum + i i = i + 1 loop 方法3 do sum = sum + i i = i + 1 loop while i < 100

VB编程求1--100之间的素数之和。。
Static a Dim c As Integer, d As Integer, bz As Integer For c = 1 To 100 bz = False If c \/ 2 = c \\ 2 Then bz = True End If For d = 3 To Int(Sqr(c)) Step 2 If c \/ d = c \\ d Then bz = True d = c End If DoEvents Next d If bz = False Then a...

相似回答