r/visualbasic • • Oct 23 '19

VB.NET Help Coin flipping program

Post image
5 Upvotes

8 comments sorted by

View all comments

0

u/ABitOffTheTop Oct 23 '19

I've left a fairly obvious bug in there, so best not submit it immediately if it is classwork. If you realise what the bug is, then you probably know enough to submit it. Still cheating though, but least you had to work for it..

 Dim NumberOfFlips As Integer = 1000
 Dim Heads As Integer
 Dim rnd As New Random
 Enum Coin
     Heads
     Tails
 End Enum

Sub Main()
 Dim CoinFlip = Function()
   If (rnd.Next(2) = 1) Then
    Return Coin.Heads
   Else
    Return Coin.Tails
   End If
 End Function

 Dim CoinFlipRecorder = Function()
   If (CoinFlip() = Coin.Heads) Then
    Heads = Heads + 1
   End If
   Return CoinFlip()
 End Function
 
 For i = 1 To NumberOfFlips
   Console.Write(CoinFlipRecorder() + " ")
 Next
 Console.WriteLine(Convert.ToString(NumberOfFlips) + " Coin Flips, " _
  + Convert.ToString(Heads) + " were heads and " _
  + Convert.ToString(NumberOfFlips - Heads) + " were tails")

End Sub