Question Binary Search Guessing Game

pickslides

New member
Joined
Jul 11, 2019
Messages
2
Programming Experience
3-5
I have designed a form using 2 textboxes and a button for a guessing game.

The user thinks of a number between 1 and 100 and the computer guesses the number.

The user replies with Y if the computer guesses, L if the number is lower and H is the number is higher

Code is not working as desired, I have no End If but the complier still runs it.

GuessForm.PNG


VB.NET:
Public Class Form1

    Dim low As Integer
    Dim guess As Integer
    Dim high As Integer

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        low = 1
        high = 100
        guess = Math.Ceiling((high + low) / 2)
        TextBox1.Text = "Is your number" & guess & "?"

        Do Until TextBox2.Text = "Y"

            guess = Math.Ceiling((high + low) / 2)

            TextBox1.Text = "Is your number" & guess & "?"


            If TextBox2.Text = "L" Then high = guess
            If TextBox2.Text = "H" Then low = guess

        Loop
    End Sub
End Class
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,108
Location
Sydney, Australia
Programming Experience
10+
Code is not working as desired
Perhaps you could explain EXACTLY how you want it to work and EXACTLY what it IS doing. If you haven't debugged the code, i.e. set a breakpoint and stepped through the code, you should do that first.
 
Top Bottom