Visual Basic随机化数字并将其设置为歌曲

时间:2014-10-20 22:04:59

标签: vb.net

有人可以帮助我使用visual basic程序。我需要一个随机数生成器来选择随机数并根据这些数字播放某些歌曲。我不记得如何为每首歌设置一个数字。我知道我需要一些if if then声明。单击按钮时会生成随机歌曲。有谁知道如何做到这一点?我需要整个代码,包括dim 1作为整数。这是我到目前为止,我知道这是完全错误的。提前致谢

Public Class NameTheArtist

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim number As Integer
        Dim rndnumber As Random
        rndnumber = New Random
        number = rndnumber.Next(1, 10)


        Dim 1  As Integer
        Dim 2 As Integer
        Dim 3 As Integer
        Dim 4 As Integer
        Dim 6 As Integer
        Dim 7 As Integer
        Dim 8 As Integer
        Dim 9 As Integer
        Dim 10 As Integer
        Dim 5 As Integer 

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click



    Public Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Integer
        Dim Generator As System.Random = New System.Random()
        Return Generator.Next(1, 10)
    End Function

    Public Function Play(ByVal Num As Integer)
        If num = 1 Then
            My.Computer.Audio.Play(My.Resources.Black_Widow, AudioPlayMode.Background)
            Threading.Thread.Sleep(5000)
            Return True
        ElseIf num = 2 Then
            My.Computer.Audio.Play(My.Resources.music2, AudioPlayMode.Background)
            Threading.Thread.Sleep(5000)
            Return True
        End If
        If number = 1 Then My.Computer.Audio.Play(My.Resources.Black_Widow, AudioPlayMode.Background)
        My.Computer.Audio.Play(My.Resources.All_about_that_bass, AudioPlayMode.Background)
        My.Computer.Audio.Play(My.Resources.Trumpets, AudioPlayMode.Background)
        My.Computer.Audio.Play(My.Resources.Love_story, AudioPlayMode.Background)
        Return False
    End Function
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

我想你正在寻找这样的东西:

Public Function Play(ByVal Num As Integer) as Boolean
Select Case num
Case 1
    My.Computer.Audio.Play(My.Resources.Black_Widow, AudioPlayMode.Background)
    Threading.Thread.Sleep(5000)
    Return True    
Case 2
    My.Computer.Audio.Play(My.Resources.music2, AudioPlayMode.Background)
    Threading.Thread.Sleep(5000)
    Return True  
Case 3
    My.Computer.Audio.Play(My.Resources.whatever, AudioPlayMode.Background)
    Threading.Thread.Sleep(5000)
    Return True
Case n
    My.Computer.Audio.Play(My.Resources.song_n, AudioPlayMode.Background)
    Threading.Thread.Sleep(5000)
    Return True
End Select 
Return False
End Function

为您获得的每个数字/歌曲创建一个案例。我没看到你声明的整数是什么。你可以删除它们来清理你的代码。