vb.net单击按钮产生100个3位随机正整数,保存为一个顺序文件,vb.net,在线等,挺急的

如题所述

Imports System.IO

Public Class Form1
    Dim strFilePath As String = "C:\1.txt"

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim a As New Random
        Dim sw As StreamWriter = New StreamWriter(strFilePath, False)
        Randomize()
        For i = 1 To 100
            sw.WriteLine(a.Next(100, 999))
            sw.Flush()
        Next
        sw.Close()
        sw = Nothing
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim sr As StreamReader = New StreamReader(strFilePath)
        For i = 1 To 100
            Dim a As Integer = Convert.ToInt16(sr.ReadLine)
            If a Mod 2 = 1 Then
                ListBox1.Items.Add(a.ToString)
            End If
        Next
        sr.Close()
        sr = Nothing
    End Sub
End Class

追问

谢谢啊,后面还有个button2把所有的奇数都显示在一个列表框中,跪求,谢谢大神

追答

Imports System.IO

Public Class Form1
Dim strFilePath As String = "C:\1.txt"

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a As New Random
Dim sw As StreamWriter = New StreamWriter(strFilePath, False)
Randomize()
For i = 1 To 100
sw.WriteLine(a.Next(100, 999))
sw.Flush()
Next
sw.Close()
sw = Nothing
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim sr As StreamReader = New StreamReader(strFilePath)
Do While Not sr.EndOfStream
Dim a As Integer = Convert.ToInt16(sr.ReadLine)
If a Mod 2 = 1 Then
ListBox1.Items.Add(a.ToString)
End If
Loop
sr.Close()
sr = Nothing
End Sub
End Class

追问

太感谢了太感谢了

温馨提示:答案为网友推荐,仅供参考
相似回答