菜鸟请教高手,在线等!!!!菜鸟请教高手,在线等!!!!菜鸟请教高手,在线等!!!!
VB.NET读取文件中的数字并相加或者排序,如何操作,用到哪些类?如,(Button1)随机生成10个数的文件\Exam.txt,然后(Button2)将10个数读出相加,(Button3)将结果追加至文件,怎么做?下面的程序对否?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myfile As New FileStream("\Exam.txt", FileMode.OpenOrCreate, FileAccess.Write)
Dim mybwriter As New BinaryWriter(myfile, System.Text.Encoding.Unicode)
Dim i, number As Integer
For i = 0 To 9
number = 10 + Int(90 * Rnd())
mybwriter.Write(number)
Next i
mybwriter.Close()
End Sub
Public Shared sum As Integer
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim myfile As New FileStream("Exam.txt", FileMode.Open, FileAccess.Read)
Dim mybreader As New BinaryReader(myfile,system.Text.Encoding.Unicode)
Dim i, number() As Integer
For i = 0 To 9
number(i) = mybreader.ReadInt32()
sum = sum + number(i)
Next i
mybreader.Close()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim myfile As New FileStream("\Exam.txt", FileMode.OpenOrCreate, FileAccess.Write)
Dim mybwriter As New BinaryWriter(myfile, System.Text.Encoding.Unicode)
mybwriter.Write(sum)
mybwriter.Close()
End Sub
mybwriter.Close()
End Sub --------------------编程问答-------------------- 随机数就random
一般的文件操作用写用:System.IO.StreamWriter
读用:System.IO.StreamReader 这两个比较简单.
dim b as new random
dim i as int32
i=b.Next (10000,99999)
b.next取得两个数中间的随机数.(随机数在MIN和MAX范围之内)
补充:.NET技术 , VB.NET