Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 随机索引发生器_Vb.net_Visual Studio 2012_Random - Fatal编程技术网

Vb.net 随机索引发生器

Vb.net 随机索引发生器,vb.net,visual-studio-2012,random,Vb.net,Visual Studio 2012,Random,好的,所以我正在尝试制作一个程序来帮助我在下学期之前掌握我的Visual Basic技能 因此,我决定创建一个随机手机号码生成器,每当我选择“Bethany”作为我的城市时,我都会遇到一个问题,无论我运行多少次,它都只拉“342”,而不是随机索引。下面是我的代码,也许你能帮我 '====================================================== 'Adam W. Schneider 'Random Cellphone number Generator

好的,所以我正在尝试制作一个程序来帮助我在下学期之前掌握我的Visual Basic技能

因此,我决定创建一个随机手机号码生成器,每当我选择“Bethany”作为我的城市时,我都会遇到一个问题,无论我运行多少次,它都只拉“342”,而不是随机索引。下面是我的代码,也许你能帮我

'======================================================

'Adam W. Schneider

'Random Cellphone number Generator

'VB 1 Refresh

'======================================================



Public Class vbform1

    'Clears the list box.

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles btnClear.Click

        'Clear the list Boxes

        lstPhoneNumbers.Items.Clear()
        lstBoxCity.Items.Clear()

    End Sub

    'Closes the Form.

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles btnExit.Click

        Me.Close()

    End Sub

    Private Sub btnExecute_Click(sender As Object, e As EventArgs) Handles btnExecute.Click

        'Determines the area code selected

        If lstAreaCodes.SelectedItem.ToString = "513" Then

            'Clears the box of previous results before displaying new results

            lstBoxCity.Items.Clear()

            'Writes the cities available for area code 513

            lstBoxCity.Items.Add("Cincinnati")
            lstBoxCity.Items.Add("Middletown")
            lstBoxCity.Items.Add("Lebanon (Warren)")
            lstBoxCity.Items.Add("South Lebanon")
            lstBoxCity.Items.Add("Mason")
            lstBoxCity.Items.Add("Little Miami")
            lstBoxCity.Items.Add("Oxford")
            lstBoxCity.Items.Add("Bethel")
            lstBoxCity.Items.Add("Harrison")
            lstBoxCity.Items.Add("Clermont")
            lstBoxCity.Items.Add("Bethany")
            lstBoxCity.Items.Add("Butlerville")
            lstBoxCity.Items.Add("Fayetteville")
            lstBoxCity.Items.Add("Felicity")
            lstBoxCity.Items.Add("Monroe")
            lstBoxCity.Items.Add("Morning Sun")
            lstBoxCity.Items.Add("Morrow")
            lstBoxCity.Items.Add("Newtonsville")
            lstBoxCity.Items.Add("Reily")
            lstBoxCity.Items.Add("Seven Mile")
            lstBoxCity.Items.Add("Shandon")
            lstBoxCity.Items.Add("Trenton")
            lstBoxCity.Items.Add("Waynesville")
            lstBoxCity.Items.Add("Williamsburg")

            'Determines the area code selected

        ElseIf lstAreaCodes.SelectedItem.ToString = "937" Then

            'Clears the box of previous results before displaying new results

            lstBoxCity.Items.Clear()

            'Writes the cities available for area code 937

            lstBoxCity.Items.Add("Aberdeen")
            lstBoxCity.Items.Add("Anna")
            lstBoxCity.Items.Add("Ansonia")
            lstBoxCity.Items.Add("Arcanum")
            lstBoxCity.Items.Add("Beavercreek")
            lstBoxCity.Items.Add("Belfast")
            lstBoxCity.Items.Add("Bellbrook")
            lstBoxCity.Items.Add("Belle Center")
            lstBoxCity.Items.Add("Bellefontaine")
            lstBoxCity.Items.Add("Blanchester")
            lstBoxCity.Items.Add("Botkins")
            lstBoxCity.Items.Add("Bowersville")
            lstBoxCity.Items.Add("Bradford")
            lstBoxCity.Items.Add("Brookville")
            lstBoxCity.Items.Add("Byhalia")
            lstBoxCity.Items.Add("Camden")
            lstBoxCity.Items.Add("Catawba")
            lstBoxCity.Items.Add("Cedarville")
            lstBoxCity.Items.Add("Centerville (Mongomery)")
            lstBoxCity.Items.Add("Christiansburg")
            lstBoxCity.Items.Add("Clarksville")
            lstBoxCity.Items.Add("Covington")
            lstBoxCity.Items.Add("Danville (Highland)")
            lstBoxCity.Items.Add("Dayton")
            lstBoxCity.Items.Add("De Graff")
            lstBoxCity.Items.Add("Decatur")
            lstBoxCity.Items.Add("Donnelsville")
            lstBoxCity.Items.Add("East Liberty")
            lstBoxCity.Items.Add("Eaton")
            lstBoxCity.Items.Add("Eldorado")
            lstBoxCity.Items.Add("Englewood")
            lstBoxCity.Items.Add("Enon")
            lstBoxCity.Items.Add("Fairborn")
            lstBoxCity.Items.Add("Famersville")
            lstBoxCity.Items.Add("Fletcher-Lena")
            lstBoxCity.Items.Add("Fort Loramie")
            lstBoxCity.Items.Add("Franklin")
            lstBoxCity.Items.Add("Georgetown")
            lstBoxCity.Items.Add("Germantown")
            lstBoxCity.Items.Add("Gettysburg")
            lstBoxCity.Items.Add("Gratis")
            lstBoxCity.Items.Add("Greenfield")
            lstBoxCity.Items.Add("Greenville")
            lstBoxCity.Items.Add("Hamersville")
            lstBoxCity.Items.Add("Higginsport")
            lstBoxCity.Items.Add("Hillsboro")
            lstBoxCity.Items.Add("Hollansburg")
            lstBoxCity.Items.Add("Huntsville")
            lstBoxCity.Items.Add("Jackson Center")
            lstBoxCity.Items.Add("Jamestown")

            'Determines the area code selected

        ElseIf lstAreaCodes.SelectedItem.ToString = "859" Then

            'Clears the box of previous results before displaying new results

            lstBoxCity.Items.Clear()

            'Writes the cities available for area code 859

            lstBoxCity.Items.Add("Alexandria")
            lstBoxCity.Items.Add("Berea")
            lstBoxCity.Items.Add("Boone")
            lstBoxCity.Items.Add("Bryantsville")
            lstBoxCity.Items.Add("Burgin")
            lstBoxCity.Items.Add("Butler")
            lstBoxCity.Items.Add("Carlisle")
            lstBoxCity.Items.Add("Cornishville")
            lstBoxCity.Items.Add("Convington")
            lstBoxCity.Items.Add("Cythiana")
            lstBoxCity.Items.Add("Danville")
            lstBoxCity.Items.Add("Falmouth")
            lstBoxCity.Items.Add("Ford")
            lstBoxCity.Items.Add("Glencole")
            lstBoxCity.Items.Add("Harrodsburg")
            lstBoxCity.Items.Add("Independence")
            lstBoxCity.Items.Add("Junction City")
            lstBoxCity.Items.Add("Kirksville")
            lstBoxCity.Items.Add("Lancaster")
            lstBoxCity.Items.Add("Lexington")
            lstBoxCity.Items.Add("Little Rock")
            lstBoxCity.Items.Add("Mackville")
            lstBoxCity.Items.Add("Midway")
            lstBoxCity.Items.Add("Millersburg")
            lstBoxCity.Items.Add("Mooresville")
            lstBoxCity.Items.Add("North Middletown")
            lstBoxCity.Items.Add("Paint Lick")
            lstBoxCity.Items.Add("Paris")
            lstBoxCity.Items.Add("Perryville")
            lstBoxCity.Items.Add("Richmond")
            lstBoxCity.Items.Add("Salvisa")
            lstBoxCity.Items.Add("Springfield")
            lstBoxCity.Items.Add("Versailles")
            lstBoxCity.Items.Add("Waco")
            lstBoxCity.Items.Add("Walton")
            lstBoxCity.Items.Add("Warsaw")
            lstBoxCity.Items.Add("Williamstown")
            lstBoxCity.Items.Add("Willisburg")
            lstBoxCity.Items.Add("Wilmore")
            lstBoxCity.Items.Add("Winchester")

            'Determines the area code selected

        ElseIf lstAreaCodes.SelectedItem.ToString = "812" Then

            'Clears the box of previous results before displaying new results

            lstBoxCity.Items.Clear()

            'Writes the cities available for area code 812

            lstBoxCity.Items.Add("Alfordsville-Glendale")
            lstBoxCity.Items.Add("Aurora")
            lstBoxCity.Items.Add("Austin")
            lstBoxCity.Items.Add("Brandon")
            lstBoxCity.Items.Add("Batesville")
            lstBoxCity.Items.Add("Bedford")
            lstBoxCity.Items.Add("Bicknell")
            lstBoxCity.Items.Add("Birdseye")
            lstBoxCity.Items.Add("Bloomfield")
            lstBoxCity.Items.Add("Boonville")
            lstBoxCity.Items.Add("Brazil")
            lstBoxCity.Items.Add("Brownstown")
            lstBoxCity.Items.Add("Bruceville")
            lstBoxCity.Items.Add("Butlerville")
            lstBoxCity.Items.Add("Campbellsburg")
            lstBoxCity.Items.Add("Canaan")
            lstBoxCity.Items.Add("Center Point")
            lstBoxCity.Items.Add("Central")
            lstBoxCity.Items.Add("Chandler")
            lstBoxCity.Items.Add("Charlestown")
            lstBoxCity.Items.Add("Chrisney")
            lstBoxCity.Items.Add("Clay City")
            lstBoxCity.Items.Add("Clearspring")
            lstBoxCity.Items.Add("Columbus")
            lstBoxCity.Items.Add("Cory")
            lstBoxCity.Items.Add("Corydon")
            lstBoxCity.Items.Add("Crandall")
            lstBoxCity.Items.Add("Crane")
            lstBoxCity.Items.Add("Cross Plains")
            lstBoxCity.Items.Add("Crothersville")
            lstBoxCity.Items.Add("Cynthiana")
            lstBoxCity.Items.Add("Dale")
            lstBoxCity.Items.Add("Decker")
            lstBoxCity.Items.Add("Dillsboro")
            lstBoxCity.Items.Add("Dubois")
            lstBoxCity.Items.Add("Dugger")
            lstBoxCity.Items.Add("East Enterprise")
            lstBoxCity.Items.Add("Edinburgh")
            lstBoxCity.Items.Add("Elberfeld")
            lstBoxCity.Items.Add("Elizabeth")
            lstBoxCity.Items.Add("Elizabethtown")
            lstBoxCity.Items.Add("Elletsville")
            lstBoxCity.Items.Add("Elnora")
            lstBoxCity.Items.Add("English")
            lstBoxCity.Items.Add("Evansville")
            lstBoxCity.Items.Add("Fairbanks")
            lstBoxCity.Items.Add("Farmersburg")
            lstBoxCity.Items.Add("Ferdinand")
            lstBoxCity.Items.Add("Flat Rock")

            'Determines the area code selected

        ElseIf lstAreaCodes.SelectedItem.ToString = "765" Then

            'Clears the box of previous results before displaying new results

            lstBoxCity.Items.Clear()

            'Writes the cities available for area code 765

            lstBoxCity.Items.Add("Albany")
            lstBoxCity.Items.Add("Alexandria")
            lstBoxCity.Items.Add("Amboy")
            lstBoxCity.Items.Add("Anderson")
            lstBoxCity.Items.Add("Arlington")
            lstBoxCity.Items.Add("Atlanta")
            lstBoxCity.Items.Add("Attica")
            lstBoxCity.Items.Add("Battle Ground")
            lstBoxCity.Items.Add("Bloomingdale")
            lstBoxCity.Items.Add("Bloutsville")
            lstBoxCity.Items.Add("Buck Creek")
            lstBoxCity.Items.Add("Bunker Hill")
            lstBoxCity.Items.Add("Burlington")
            lstBoxCity.Items.Add("Cambridge City")
            lstBoxCity.Items.Add("Carthage")
            lstBoxCity.Items.Add("Cayuga")
            lstBoxCity.Items.Add("Centerville")
            lstBoxCity.Items.Add("Chesterfield")
            lstBoxCity.Items.Add("Clarks Hill")
            lstBoxCity.Items.Add("Clinton")
            lstBoxCity.Items.Add("Cloverdale")
            lstBoxCity.Items.Add("Coatesville")
            lstBoxCity.Items.Add("Colfax")
            lstBoxCity.Items.Add("Connersville")
            lstBoxCity.Items.Add("Covington")
            lstBoxCity.Items.Add("Crawfordsville")
            lstBoxCity.Items.Add("Cutler")
            lstBoxCity.Items.Add("Dana")
            lstBoxCity.Items.Add("Darlington")
            lstBoxCity.Items.Add("Delphi")
            lstBoxCity.Items.Add("Denver")
            lstBoxCity.Items.Add("Dunkirk")
            lstBoxCity.Items.Add("Eaton")
            lstBoxCity.Items.Add("Elwood")
            lstBoxCity.Items.Add("Eminence")
            lstBoxCity.Items.Add("Fairmount")
            lstBoxCity.Items.Add("Farmland")
            lstBoxCity.Items.Add("Fillmore")
            lstBoxCity.Items.Add("Fountain City")
            lstBoxCity.Items.Add("Fowler")
            lstBoxCity.Items.Add("Frankfort")
            lstBoxCity.Items.Add("Frankton")
            lstBoxCity.Items.Add("Gaston")
            lstBoxCity.Items.Add("Geetingsville")
            lstBoxCity.Items.Add("Glenwood")
            lstBoxCity.Items.Add("Greencastle")
            lstBoxCity.Items.Add("Greens Fork")

        End If




    End Sub

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

        'Calculate Random Prefix based upon selected city

        If lstBoxCity.SelectedItem.ToString = "Bethany" Then

            Dim Bethany As String() = {"298", "342", "443", "644", "712", "755", "759", "777", "779", "847"}
            Dim randomBethany As String = Bethany(CInt(Rnd(Bethany.Count)))

            MsgBox(randomBethany.ToString)

        End If
    End Sub
End Class

Rnd
方法总是返回一个介于0和1之间的数字。如果你想要一个更高的数字,你需要将它乘以一些东西。但是,您确实应该使用
Random
类来生成随机数。
Rnd
方法是一种旧的VB6风格的方法,主要用于向后兼容。
Random
类也有一个简单得多的语法,您可以指定希望结果落在其中的数字范围

Dim r As New Random()
Dim randomBethany As String = Bethany(r.Next(0, Bethany.Count - 1))

但是,当表单加载时,您应该创建一次
随机
对象,因为每次创建一个新的随机数生成算法时,它都会对随机数生成算法进行重新播种。

在拾取随机数之前,您需要调用
随机化()
;否则每次都会得到相同的随机数,因为种子是相同的,如下所示:

' Initialize the random-number generator.
Randomize()

Dim Bethany As String() = {"298", "342", "443", "644", "712", "755", "759", "777", "779", "847"}

' Generate random value between 1 and then length of your Bethany array
Dim randomBethany As String = Bethany(Int((Bethany.Count * Rnd()) + 1))


阅读以了解更多信息。

将来,如果你能给出一个更简洁的示例,你会得到更好的回答。这非常适合生成随机索引,但不适合生成随机索引中的信息。虽然我同意你的观点,我不应该使用Rnd,但卡尔·安德森的工作是为了我的特殊情况。对不起。我只是想让这个例子尽可能简单。我更新了我的答案,以显示如何从该随机索引处的数组中获取字符串。使用
Random
类实际上更简单,因为它实际上返回一个
整数
,所以您不需要调用
CInt
。这回答了我的问题,并且完全按照我的要求执行,现在只需使用do While循环执行10次并写入列表框即可。祝我好运,我会带着问题回来。@user3119737-很高兴这对你有帮助,如果你觉得它回答了你的问题,请接受答案和/或投票支持答案。:-)建议您不要在问题的“仅一个问题”类型更新中附带问题。使用新代码和新问题打开新问题。若要接受答案,请在答案旁边查找复选标记,单击该复选标记时,它将变为绿色。选中标记上方的向上箭头是您单击向上投票的对象。