代码:
成都创新互联是一家专注于网站建设、成都做网站与策划设计,新干网站建设哪家好?成都创新互联做网站,专注于网站建设10余年,网设计领域的专业建站公司;建站业务涵盖:新干等地区。新干做网站价格咨询:18982081108
Imports System.IO
Public Class feibo
Function fancibo(ByVal n As Integer) As Long
If n = 2 Then
Return 1
Else
Return fancibo(n - 1) + fancibo(n - 2)
End If
End Function
Sub CreateData(ByVal max As Integer)
Dim fs As New FileStream("fb.txt", FileMode.Create, FileAccess.Write)
Dim mywriter As New BinaryWriter(fs)
Dim i As Integer
For i = 1 To max
mywriter.Write(fancibo(i))
Next i
mywriter.Close()
End Sub
Sub ReadandDealData()
Dim fs As New FileStream("fb.txt", FileMode.Open, FileAccess.Read)
Dim myreader As New BinaryReader(fs)
Dim i, s As Integer
Dim tot As Long
Dim avg As Single
i = 0 : s = 0 : tot = 0 : avg = 0.0
ListBox1.Items.Clear()
While fs.Position fs.Length
s = myreader.ReadInt64()
ListBox1.Items.Add(s)
tot += s
i += 1
End While
avg = tot / i
ListBox1.Items.Add("数据的总和:" tot)
ListBox1.Items.Add("数据的平均值:" avg)
myreader.Close()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
CreateData(15)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ReadandDealData()
End Sub
End Class
因为您计算的数值太大,超过了整数的最上限。
而如果您用LONG数据类型或ULONG数据类型,也不会计算超过8次的结果。
vb.net和vb6.0不同,无法直接使用控件数组。不过可以通过其他方式变通一下。
比如现在有10个label,要将这10个label的text属性统一设置为“这是第X个标签”(X为1-10)。
1.建立10个label,名称分别为label1、label2、label3。。label10
2.代码:
For i = 1 To 10
Me.FindControl("label" i).Text = "这是第" i “个标签”
Next i
关键点是Me.FindControl()的方法,在代码中Me.FindControl("label1").text与label1.text是一样的。
代码:
Sub Main()
Dim a(20) As Double
a(0) = 1
a(1) = 2
For i = 2 To 19
a(i) = (a(i - 1) + a(i - 2)) * 1.5
Next
Console.Write("数列是:")
Console.WriteLine()
For i = 0 To 19
Console.Write(a(i))
Console.Write(" ")
Next
Console.WriteLine()
Console.Write("第18项与第6项的差是: ")
Console.Write(a(17) - a(5))
End Sub
测试结果:
数列是:
1 2 4.5 9.75 21.375 46.6875 102.09375 223.171875 487.8984375 1066.60546875 2331.
755859375 5097.5419921875 11143.9467773438 24362.2331542969 53259.2698974609 116
432.254577637 254537.286712646 556454.311935425 1216487.39797211 2659412.5648613
第18项与第6项的差是: 556407.624435425