Partial Class Default2
Inherits System.Web.UI.Page
Private Sub form1_Load(sender As Object, e As EventArgs) Handles form1.Load
Dim Flagimageurl(6) As String ' creating the flag image url array'
Flagimageurl(0) = "https://www.cia.gov/library/publications/the-world-factbook/graphics/flags/large/uk-lgflag.gif"
Flagimageurl(1) = "https://upload.wikimedia.org/wikipedia/en/thumb/c/c3/Flag_of_France.svg/800px-Flag_of_France.svg.png"
Flagimageurl(2) = "https://upload.wikimedia.org/wikipedia/en/thumb/0/05/Flag_of_Brazil.svg/720px-Flag_of_Brazil.svg.png"
Flagimageurl(3) = "https://upload.wikimedia.org/wikipedia/en/thumb/9/9a/Flag_of_Spain.svg/750px-Flag_of_Spain.svg.png"
Flagimageurl(4) = "https://upload.wikimedia.org/wikipedia/en/thumb/a/a4/Flag_of_the_United_States.svg/1235px-Flag_of_the_United_States.svg.png"
Flagimageurl(5) = "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a2/Flag_of_Russia_%28Kremlin.ru%29.svg/1024px-Flag_of_Russia_%28Kremlin.ru%29.svg.png"
Dim CountryNames(6) As String 'creating the country names array'
CountryNames(0) = "United Kingdom"
CountryNames(1) = "France"
CountryNames(2) = "Brazil"
CountryNames(3) = "Spain"
CountryNames(4) = "United States of America (USA)"
CountryNames(5) = "Russia"
flag.ImageUrl = Flagimageurl(6 * Rnd()) 'Choosing a random flag'
RadioButton1.Text = CountryNames(5 * Rnd()) 'Randomly picks the country according to the CountryNames array'
RadioButton2.Text = CountryNames(5 * Rnd()) ' with random * 6 you get blanks so you need to use random * 5'
RadioButton3.Text = CountryNames(5 * Rnd())
RadioButton4.Text = CountryNames(5 * Rnd())
If RadioButton1.Text = RadioButton2.Text Then 'Makes sure that the radiobuttons don't show duplicate answers'
RadioButton2.Text = CountryNames(5 * Rnd())
End If
If RadioButton2.Text = RadioButton1.Text Then
RadioButton1.Text = CountryNames(5 * Rnd())
End If
If RadioButton3.Text = RadioButton1.Text Or RadioButton3.Text = RadioButton2.Text Then
RadioButton3.Text = CountryNames(5 * Rnd())
End If
If RadioButton4.Text = RadioButton1.Text Or RadioButton2.Text = RadioButton3.Text Then
RadioButton4.Text = CountryNames(5 * Rnd())
End If
End Sub
End Class
问题在于,当我运行代码时,四个答案中的两个是相同的。或者它做的另一件事是它并不总是有正确的答案。
如何解决此问题?
答案 0 :(得分:4)
首先,我会考虑使用Dictionary代替:
Dim countries As New Dictionary(Of String, String) _
From {{"United Kingdom", "https://www.cia.gov/library/publications/the-world-factbook/graphics/flags/large/uk-lgflag.gif"},
{"France", "https://upload.wikimedia.org/wikipedia/en/thumb/c/c3/Flag_of_France.svg/800px-Flag_of_France.svg.png"},
{"Brazil", "https://upload.wikimedia.org/wikipedia/en/thumb/0/05/Flag_of_Brazil.svg/720px-Flag_of_Brazil.svg.png"},
{"Spain", "https://upload.wikimedia.org/wikipedia/en/thumb/9/9a/Flag_of_Spain.svg/750px-Flag_of_Spain.svg.png"},
{"United States of America (USA)", "https://upload.wikimedia.org/wikipedia/en/thumb/a/a4/Flag_of_the_United_States.svg/1235px-Flag_of_the_United_States.svg.png"},
{"Russia", "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a2/Flag_of_Russia_%28Kremlin.ru%29.svg/1024px-Flag_of_Russia_%28Kremlin.ru%29.svg.png"}}
然后我会使用Random类,将该项添加到另一个Dictionary
并从当前Dictionary
中删除该项,以便不能再次使用它。我还在循环使用六个RadioButton
控件来减少代码,但您可以分开并依次执行每个RadioButton
。这取决于您的设计:
Dim countriesSelected As New Dictionary(Of String, String)
Dim countryRandom As New Random
For Each rd As RadioButton In Me.Controls.OfType(Of RadioButton)()
Dim nextRandom As Integer = countryRandom.Next(countries.Count)
rd.Text = countries.Keys(nextRandom)
countriesSelected.Add(countries.Keys(nextRandom), countries.Values(nextRandom))
countries.Remove(countries.Keys(nextRandom))
Next
Dim flagRandom As New Random
flag.ImageUrl = countriesSelected.Values(flagRandom.Next(countriesSelected.Count))
输出:
如果你想在这里分开,那就是代码:
Dim countryRandom As New Random
Dim nextRandom As Integer = countryRandom.Next(countries.Count)
RadioButton1.Text = countries.Keys(nextRandom)
countriesSelected.Add(countries.Keys(nextRandom), countries.Values(nextRandom))
countries.Remove(countries.Keys(nextRandom))
nextRandom = countryRandom.Next(countries.Count)
RadioButton2.Text = countries.Keys(nextRandom)
countriesSelected.Add(countries.Keys(nextRandom), countries.Values(nextRandom))
countries.Remove(countries.Keys(nextRandom))
nextRandom = countryRandom.Next(countries.Count)
RadioButton3.Text = countries.Keys(nextRandom)
countriesSelected.Add(countries.Keys(nextRandom), countries.Values(nextRandom))
countries.Remove(countries.Keys(nextRandom))
nextRandom = countryRandom.Next(countries.Count)
RadioButton4.Text = countries.Keys(nextRandom)
countriesSelected.Add(countries.Keys(nextRandom), countries.Values(nextRandom))
countries.Remove(countries.Keys(nextRandom))
您可以看到我选择循环RadioButton
控件的原因。阅读和维护起来要简单得多。
如果您真的必须使用Array
,正如您在评论中所说,那么我会尝试为您做到这一点。代码是丑陋的,可能更难理解,至少在我看来它应该给你你想要的东西。
首先将(6)
更改为(5)
,因为这会创建6个项目。 Array
基于零,因此0是第一项不是1.两个数组中的索引必须相同,以便我们在结尾选择正确的标志。
Dim Flagimageurl(5) As String ' creating the flag image url array'
Flagimageurl(0) = "https://www.cia.gov/library/publications/the-world-factbook/graphics/flags/large/uk-lgflag.gif"
Flagimageurl(1) = "https://upload.wikimedia.org/wikipedia/en/thumb/c/c3/Flag_of_France.svg/800px-Flag_of_France.svg.png"
Flagimageurl(2) = "https://upload.wikimedia.org/wikipedia/en/thumb/0/05/Flag_of_Brazil.svg/720px-Flag_of_Brazil.svg.png"
Flagimageurl(3) = "https://upload.wikimedia.org/wikipedia/en/thumb/9/9a/Flag_of_Spain.svg/750px-Flag_of_Spain.svg.png"
Flagimageurl(4) = "https://upload.wikimedia.org/wikipedia/en/thumb/a/a4/Flag_of_the_United_States.svg/1235px-Flag_of_the_United_States.svg.png"
Flagimageurl(5) = "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a2/Flag_of_Russia_%28Kremlin.ru%29.svg/1024px-Flag_of_Russia_%28Kremlin.ru%29.svg.png"
Dim CountryNames(5) As String 'creating the country names array'
CountryNames(0) = "United Kingdom"
CountryNames(1) = "France"
CountryNames(2) = "Brazil"
CountryNames(3) = "Spain"
CountryNames(4) = "United States of America (USA)"
CountryNames(5) = "Russia"
我创建了一个单独的Array
,试图保留已被选中的国家/地区。
Dim selectedCountries(3) As String
仍然使用Random
类并循环浏览RadioButton
控件,这次我们还必须遍历selectedCountries
以确保我们不会重复。我初始化addCountry
的值为False
,在我初始化循环后将其设置为True
,然后再次设置为False
如果我发现该国家已经被选中。然后,这将继续循环,直到选择了新的国家/地区。
Dim countryRandom As New Random
Dim i As Integer = 0
For Each rb As RadioButton In Me.Controls.OfType(Of RadioButton)()
Dim randomNumber As Integer
Dim addCountry As Boolean = False
Do While addCountry = False
randomNumber = countryRandom.Next(CountryNames.Count)
addCountry = True
For Each selectedCountry In selectedCountries
If selectedCountry = randomNumber Then
addCountry = False
Exit For
End If
Next
Loop
rb.Text = CountryNames(randomNumber)
selectedCountries(i) = randomNumber
i += 1
Next
然后我对Array
:
Array.Sort(selectedCountries)
我看一下随机数,然后检查该随机数是否在selectedCountries
Array
中。然后我用它:
Dim flagRandom As New Random
Dim selectedFlag As String = ""
Do While selectedFlag = ""
Dim randomNumber As Integer = flagRandom.Next(Flagimageurl.Count)
For Each selectedCountry In selectedCountries
If selectedCountry = randomNumber Then
selectedFlag = Flagimageurl(randomNumber)
End If
Next
Loop
flag.ImageUrl = selectedFlag
这应该会给你你所追求的,但我不能强调这段代码的实际效率。
答案 1 :(得分:0)
这样做的一种方法是将所有If
语句替换为下面的3个循环 - 这不是最好的方法,但是对于您的程序而言,它不会影响性能
代码检查按钮2,3和4与其他按钮不同。你不需要检查按钮1,因为循环将检查所有后续按钮是否与它相同。
每个循环都会从数组中选择一个新的随机文本字符串。如果它与任何其他按钮相同,它将选择另一个按钮,直到它们与所有按钮不同。
Do While RadioButton2.Text = RadioButton1.Text Or RadioButton2.Text = RadioButton3.Text Or RadioButton2.Text = RadioButton4.Text 'Makes sure that the radiobuttons don't show duplicate answers'
RadioButton1.Text = CountryNames(5 * Rnd())
Loop
Do While RadioButton3.Text = RadioButton2.Text Or RadioButton3.Text = RadioButton1.Text Or RadioButton3.Text = RadioButton4.Text 'Makes sure that the radiobuttons don't show duplicate answers'
RadioButton1.Text = CountryNames(5 * Rnd())
Loop
Do While RadioButton4.Text = RadioButton2.Text Or RadioButton4.Text = RadioButton3.Text Or RadioButton4.Text = RadioButton1.Text 'Makes sure that the radiobuttons don't show duplicate answers'
RadioButton1.Text = CountryNames(5 * Rnd())
Loop
但是,您选择使用Rnd()
将不太可能生成数字5.您最好将上述代码更改为此
Dim randomNumber As New Random
flag.ImageUrl = Flagimageurl(randomNumber.Next(0, 5)) 'Choosing a random flag'
RadioButton1.Text = CountryNames(randomNumber.Next(0, 5)) 'Randomly picks the country according to the CountryNames array'
RadioButton2.Text = CountryNames(randomNumber.Next(0, 5))
RadioButton3.Text = CountryNames(randomNumber.Next(0, 5))
RadioButton4.Text = CountryNames(randomNumber.Next(0, 5))
Do While RadioButton2.Text = RadioButton1.Text Or RadioButton2.Text = RadioButton3.Text Or RadioButton2.Text = RadioButton4.Text 'Makes sure that the radiobuttons don't show duplicate answers'
RadioButton1.Text = CountryNames(randomNumber.Next(0, 5))
Loop
Do While RadioButton3.Text = RadioButton2.Text Or RadioButton3.Text = RadioButton1.Text Or RadioButton3.Text = RadioButton4.Text 'Makes sure that the radiobuttons don't show duplicate answers'
RadioButton1.Text = CountryNames(randomNumber.Next(0, 5))
Loop
Do While RadioButton4.Text = RadioButton2.Text Or RadioButton4.Text = RadioButton3.Text Or RadioButton4.Text = RadioButton1.Text 'Makes sure that the radiobuttons don't show duplicate answers'
RadioButton1.Text = CountryNames(randomNumber.Next(0, 5))
Loop
此代码同样可能生成所有数字。