如何在vb.net中创建一个随机字符串数组

时间:2015-01-05 05:52:36

标签: arrays vb.net random

我不确定我的头衔是否正是我所需要的,所以让我解释一下。

我正在做的是做一个简单的"列出美国国家(随机进入标签)然后在标签下方的游戏是5个按钮,其中我试图将文本更改为按钮以随机State Capital(所有大写字母需要随机,除了正确一个经过几个小时的研究没有运气似乎我不是唯一一个试图得到帮助的人。如果你能提供帮助就会很棒!

Private Class Players
    Public Team As String
    Public Name As String
    Public Sub New(ByVal Team As String, ByVal Name As String)
        Me.Team = Team
        Me.Name = Name
    End Sub
End Class

' Arraylist
Dim lstCapitals As New ArrayList

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

    Dim list As New List(Of Players)

    ' Capital Names
    lstCapitals.Add("Montgomery")
    lstCapitals.Add("Juneau")
    lstCapitals.Add("Phoenix")
    lstCapitals.Add("Little Rock")
    lstCapitals.Add("Sacramento")
    lstCapitals.Add("Denver")
    lstCapitals.Add("Hartford")

    ' Random number generator
    Dim randomInt As New Random

    ' Pulls a name randomly from the list
    Dim stringname As String = lstCapitals.Item(randomInt.Next(0, 6))
    '6 = lstCapitals.Count / one line up

    ' Show the name
    'stateNamelbl.Show()
     'NOT SURE IF I NEEDED THIS OR NOT THATS WHY ITS COMMENTED OUT
    'Dim RandomList = From RndList In (From xitem In list Select xitem Group By xitem.Team Into First()) _
    '      Select RndList _
    '      Order By Rnd()

    For Each item In lstCapitals
        Randomize()
        MsgBox(item.Team)
    Next

End Sub

2 个答案:

答案 0 :(得分:2)

您的示例代码并未解释您如何选择美国州,因此我假设您将在某处使用字符串中的值。这是一个简单的方法,您可以使用它来生成一个随机字符串数组来保存州首都的值:

Dim sCorrectStateCapital As String = "Denver"   ' This is hardcoded to "Denver" assuming that "Colorado" was the current US State you are playing the game for - you need to come up with your own logic to figure out how this works as I have no idea how you are choosing the State
Dim nButtonCount As Integer = 5 ' This is hardcoded to five since you have five buttons
Dim sCapitalsArray(nButtonCount - 1) As String  ' Create the string array to hold the chosen capitals

' Loop five times and choose a different state capital each time

For i As Int32 = 0 To sCapitalsArray.Length - 1
    Dim nTempIndex As Int32 = randomInt.Next(0, lstCapitals.Count - 1)  ' Save the index value of the arraylist that we are choosing
    sCapitalsArray(i) = lstCapitals.Item(nTempIndex)    ' Populate the captials array
    lstCapitals.RemoveAt(nTempIndex)    ' Remove the selected capital from the list of possible choices since we do not want to choose it again
Next

' Test to see if the capitals array already contains the "correct" value

If Not sCapitalsArray.Contains(sCorrectStateCapital) Then
    ' The correct value is not already in the list so we overwrite one of the values in the array at a random index
    sCapitalsArray(randomInt.Next(0, sCapitalsArray.Count - 1)) = sCorrectStateCapital
End If

答案 1 :(得分:1)

以下表格有......

3标签:lblState,lblScore,lblAnswer

5个按钮:btnCapital1,btnCapital2,btnCapital3,btnCapital4,btnCapital5

State Capitals Quizzer

Public Class frmStateCapitalsQuiz

    Public Class State

        Public Name As String
        Public Capital As String

        Public Sub New(ByVal data As String)
            Dim values() As String = data.Split(",")
            Me.Name = values(0)
            Me.Capital = values(1)
        End Sub

        Public Overrides Function ToString() As String
            Return Me.Capital & ", " & Me.Name
        End Function

    End Class

    Private R As New Random
    Private Count As Integer
    Private Correct As Integer
    Private States As List(Of State)
    Private CurrentState As State = Nothing
    Private CurrentStateSet As List(Of State)

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadStateData()
        GenerateQuestion()
        lblScore.Text = "Score: "
        lblAnswer.Text = ""
    End Sub

    Private Sub LoadStateData()
        States = New List(Of State)
        Dim StateData As String = "Alabama,Montgomery;Alaska,Juneau;Arizona,Phoenix;Arkansas,Little Rock;California,Sacramento;Colorado,Denver;Connecticut,Hartford;Delaware,Dover;Florida,Tallahassee;Georgia,Atlanta;Hawaii,Honolulu;Idaho,Boise;Illinois,Springfield;Indiana,Indianapolis;Iowa,Des Moines;Kansas,Topeka;Kentucky,Frankfort;Louisiana,Baton Rouge;Maine,Augusta;Maryland,Annapolis;Massachusetts,Boston;Michigan,Lansing;Minnesota,St. Paul;Mississippi,Jackson;Missouri,Jefferson City;Montana,Helena;Nebraska,Lincoln;Nevada,Carson City;New Hampshire,Concord;New Jersey,Trenton;New Mexico,Santa Fe;New York,Albany;North Carolina,Raleigh;North Dakota,Bismarck;Ohio,Columbus;Oklahoma,Oklahoma City;Oregon,Salem;Pennsylvania,Harrisburg;Rhode Island,Providence;South Carolina,Columbia;South Dakota,Pierre;Tennessee,Nashville;Texas,Austin;Utah,Salt Lake City;Vermont,Montpelier;Virginia,Richmond;Washington,Olympia;West Virginia,Charleston;Wisconsin,Madison;Wyoming,Cheyenne"
        For Each pair As String In StateData.Split(";")
            States.Add(New State(pair))
        Next
    End Sub

    Private Sub GenerateQuestion()
        Dim shuffledStates = States.OrderBy(Function(x) R.NextDouble()).Take(5).ToList
        CurrentState = shuffledStates(R.Next(shuffledStates.Count))
        lblState.Text = CurrentState.Name
        btnCapital1.Text = shuffledStates(0).Capital
        btnCapital2.Text = shuffledStates(1).Capital
        btnCapital3.Text = shuffledStates(2).Capital
        btnCapital4.Text = shuffledStates(3).Capital
        btnCapital5.Text = shuffledStates(4).Capital
    End Sub

    Private Sub btnCapitals_Click(sender As Object, e As EventArgs) Handles btnCapital1.Click, btnCapital2.Click, btnCapital3.Click, btnCapital4.Click, btnCapital5.Click
        Dim SelectedCaptial As Button = DirectCast(sender, Button)

        lblAnswer.Text = CurrentState.ToString
        Count = Count + 1
        If SelectedCaptial.Text = CurrentState.Capital Then
            lblScore.BackColor = Color.Green
            Correct = Correct + 1
        Else
            lblScore.BackColor = Color.Red
        End If
        Dim percentCorrect = CInt(CDbl(Correct) / CDbl(Count) * 100)
        lblScore.Text = String.Format("Score: {0} / {1}, {2}% Correct", Correct, Count, percentCorrect)

        GenerateQuestion()
    End Sub

End Class