码迷,mamicode.com
首页 > 其他好文 > 详细

怎样算出15个数的和

时间:2016-04-10 15:00:53      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

又来了

这周事情好多

抽时间写VB

老实说

VB课上的内容没怎么听懂

特别是for each t in a的那种方法

实在是运行不出来技术分享

下面就说说我运行出来的算15个数和的几种方法

方法1

技术分享

具体程序为:

Option Explicit

Private Sub Command1_Click()

Cls

Font.Size = 70

 

Dim i As Integer

Dim a(10) As Integer

Dim sum As Integer

 

Randomize

 Fori = 1 To 10

 a(i)= Int(Rnd * 1000)

 sum= sum + a(i)

Next i

Print sum

 

End Sub


方法二

技术分享

具体程序为:

Private Sub Command1_Click()

Cls

Font.Size = 30

Dim n As Integer

Dim s As Integer

Dim a(10) As Integer

 

n = 1

While n <= 10

a(n) = Int(Rnd * 1000)

n = n + 1

Wend

 

n = 1

While n <= 10

Print a(n)

n = n + 1

Wend

 

s = 0

n = 1

While n <= 10

s = s + a(n)

n = n + 1

Wend

 

Print s

End Sub


方法三

技术分享

具体程序为:

Dim i As Integer

Dim a As Integer

Dim sum As Integer

 

Private Sub Command1_Click()

Cls

Font.Size = 30

sum = 0

For i = 1 To 10

Randomize

  a =Int(Rnd * 1000)

  sum= sum + a

 Print "10个随机数和为:" & sum

 Next

 

 

End Sub


方法四

技术分享

具体程序为:

Dim i As Integer

Dim a As Integer

Dim sum As Integer

 

Private Sub Command1_Click()

Cls

Font.Size = 30

sum = 0

i = 1

Do While i <= 10

Randomize

a = Int(Rnd * 1000)

sum = sum + a

MsgBox "10个随机数和为:"& sum

Loop

 

 

End Sub


方法五

技术分享

具体程序为:

Dim i As Integer

Dim a As Integer

Dim sum As Integer

 

Private Sub Command1_Click()

Cls

Font.Size = 30

sum = 0

 

Do Until i > 10

Randomize

a = Int(Rnd * 1000)

sum = sum + a

MsgBox "10个随机数和为:"& sum

Loop

 

 

 

End Sub


方法六

技术分享

具体程序为:

Dim i As Integer

Dim a As Integer

Dim sum As Integer

 

Private Sub Command1_Click()

Cls

Font.Size = 30

sum = 0

 

i = 1

Do

 Randomize

 a =Int(Rnd * 1000)

 sum= sum + a

 i =i + 1

Loop While i <= 10

MsgBox "10个随机数和为:"& sum

 

 

End Sub



方法七

技术分享

具体程序为:

Private Sub Command1_Click()

Cls

Font.Size = 70

Print f(10)

 

End Sub

 

Private Function f(x As Integer) As Long

  Ifx = 1 Then

    f= x

 Else

    f= x + f(x - 1)

  EndIf

End Function



方法八

技术分享

具体程序为:

Private Sub Command1_Click()

Cls

Font.Size = 70

 

Dim i As Integer

Dim a As Integer

Dim sum As Integer

 

 

Do

Randomize

 a = Int(Rnd* 1000)

 sum= sum + a

 i =i + 1

Loop Until i > 10

MsgBox "10个随机数和为:"& sum

 

End Sub



方法九

这就是我前面说的for each那个

运行不出来阿技术分享技术分享

Private Sub Command1_Click()

Cls

Font.Size = 30

Dim n As Integer

Dim s As Integer

Dim a(10) As Integer

 

n = 1

While n <= 10

a(n) = Int(Rnd * 1000)

n = n + 1

Wend

 

n = 1

While n <= 10

Print a(n)

n = n + 1

Wend

 

s = 0

n = 1

 

Dim t As Integer

For Each t In a

s = s + t

Next

 

Print s

End Sub

依稀觉得好像哪里错了....

但是又找不出来错


好了

以上就是15个数和的方法

走拉技术分享

怎样算出15个数的和

标签:

原文地址:http://blog.csdn.net/ahayummy/article/details/51104765

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!