2017-07-03 41 views
0
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

與您的問題無關,但僅供參考'Dim Flagimageurl(6)As String'創建一個包含_ ** 7項** _的數組。要創建一個包含6個項目的項目,請執行以下操作:'Dim Flagimageurl(5)As String'或更好的可讀性/理解:'Dim Flagimageurl(6 - 1)As String' - **注:**此行爲僅限於僅限VB.NET。 –

+0

取決於你如何生成隨機數的一些功能被拋出系統時間,因此你可以得到類似的結果 – Mederic

+0

mederic我不明白 –

回答

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)) 

輸出:

Output

因爲如果你想在這裏分離出來是代碼:

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) 

我看得到一個隨機數,然後檢查,看看是否能隨機數是在selectedCountriesArray。然後我使用:

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

@AlexMcIver:你寫的東西效率非常低,需要更多的If-Then-Else'語句才能工作。錯誤已經從字面上給你最簡單的答案,以避免重複。 –

0

這樣做的一種方式是用下面的3個循環替換所有的If語句 - 不是實現它的最佳方式,但對於程序而言,它不會影響性能。

該代碼檢查按鈕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 

該代碼將同樣可能產生的所有的數字。

+0

你能看看我更新的代碼:https://github.com/alexmciver/Alex-M-Flags/blob/master/Default2.aspx.vb 我在哪裏添加你的循環到我的代碼? –

+0

我在Git上創建了一個fork和pull請求 - 查看這些註釋並盡一切辦法回覆我,並回答有關這些更改的任何疑問 –

相關問題