Module GuessANumber Dim _Rnd As Random Function IsAnInt(input As String) As Boolean Dim output As Integer Return Integer.TryParse(input, output) End Function Function GetNumericInput(prompt As String) As Integer Dim val As Integer While True Console.Write($"{prompt}: ") Dim ans = Console.ReadLine() If IsAnInt(ans) Then val = Integer.Parse(ans) Exit While End If Console.WriteLine("Your entry was not a number.") End While Return val End Function Function GetInput(question As String) As String Dim ans As String = String.Empty While True Console.WriteLine($"{question}?") ans = Console.ReadLine() If Not String.IsNullOrWhiteSpace(ans) Then Exit While End If Console.WriteLine("Your entry seems empty.") End While Return ans End Function Function AskToPlayAgain() As Boolean Dim answer As Boolean = False While True Dim ans = GetInput("Would you like to play again? (""YES"" OR ""NO"")").ToUpper() If ans = "YES" Or ans = "Y" Then answer = True Exit While ElseIf ans = "NO" Or ans = "N" Then answer = False Exit While End If Console.WriteLine("Please respond with ""YES"" or ""NO""") End While Return answer End Function Sub Main() Console.WriteLine("*** GUESS A NUMBER : Visual Basic ***") _Rnd = New Random() Dim Running As Boolean = True Dim GotMaximumNumber As Boolean = False Dim GameOver As Boolean = False Dim MaxNumber As Integer = 0 Dim Answer As Integer = 0 Dim Tries As Integer = 0 While Running While Not GotMaximumNumber MaxNumber = GetNumericInput("Please enter the maximum number you wish to use") If MaxNumber > 0 Then GotMaximumNumber = True Answer = _Rnd.Next(1, MaxNumber) GameOver = False Tries = 0 Console.WriteLine($"Ok, I am thinking of a number between 1 and {MaxNumber}") Else Console.WriteLine("The maximum number should be greater than zero.") End If End While Dim Guess = GetNumericInput($"Guess a number between 1 and {MaxNumber}") Tries = Tries + 1 If Guess > Answer Then Console.WriteLine("Too High.") ElseIf Guess < Answer Then Console.WriteLine("Too Low.") Else If Tries = 1 Then Console.WriteLine("You got it in 1 try!") Else Console.WriteLine($"You got it in {Tries} tries!") End If GameOver = True End If If GameOver Then Running = AskToPlayAgain() If Running Then GotMaximumNumber = False End If End If End While Console.WriteLine("Thanks for playing") Console.WriteLine("*** GAME OVER ***") Console.ReadLine() End Sub End Module