在计算尝试时翻转硬币,直到你连续获得5个尾巴

时间:2016-10-10 11:44:42

标签: vb.net

我想做什么:

我想无限循环,直到连续5个尾巴。我还想展示能够连续实现5个尾部的尝试次数

这就是我所拥有的:

Dim number = rand.Next(1, 3)

If number = 1 Then
    RichTextBox1.Text += "Tails" & vbNewLine
ElseIf number = 2 Then
    RichTextBox1.Text += "Heads" & vbNewLine
End If

number = rand.Next(1, 3)

我还没有弄明白。提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

你需要一个计数器变量来存储已经有尾巴的频率。如果您找到了,则While tailCount < 5将打破此循环。另一个计数器将计算总尝试次数:

Dim rand As New Random()
Dim maxTailCount = 5
Dim attempts = 0
Dim tailCount = 0

While tailCount < maxTailCount
    attempts += 1
    Dim number = rand.Next(1, 3)
    If number = 1 Then
        tailCount += 1
    Else
        tailCount = 0 ' because they must be "in a row"
    End If
End While
Console.WriteLine("Found {0} tails, total attempts: {1}", maxTailCount, attempts)