Number guessing

decypher

Member
Joined
Feb 14, 2005
Messages
7
Programming Experience
Beginner
I'm making a number guessing console program. You enter Higher Lower or Correct. I have it arranged into loops, starting number is 50. If your press higher then it goes to 75, lower it would goto 25 as the next number. What i need to know is how do i limit the program. So what once the user enters Higher and the program goes to 75, it will not let you go below 51 if your keep pressing lower. If your go higher still from 75 to 88 as the next guess.. pressing lower repeatedly will not let you go below 76, how do i contain this?
 
TPM said:
Use global min/max variables.

TPM

im sorry can u explain a bit further, im a complete newb. So you set a variable outside the main program. How do i make my if statements check with that min max that is set
 
What i'm trying to do is to stop the program from going passed the number already guessed.. i can get it to stop so that it doesen't guess above 100 or below 0..
but say u go higher from 50 to 75, if i press lower my math will go below 50, which it shouldn't... this is what i got

Sub Main()

Dim num As Integer

Dim counter As Integer



Console.WriteLine("Think of a number in the range [1 .. 100] and I will guess what it is.")

Console.WriteLine("Please direct my guess with a lower or upper case response of H, L, or C")



counter = 1

Console.WriteLine("50 [H, L, C]:")

Do While Console.ReadLine <> "y"

If Console.ReadLine.ToLower = "h" Then

num = CInt(((101 - num) / 2) + num)

Console.WriteLine(
CStr(num) & (" [H, L, C]: "))

ElseIf Console.ReadLine.ToLower = "l" Then

num = CInt(num / 2)

Console.WriteLine(
CStr(num) + (" [H, L, C]: "))

ElseIf Console.ReadLine.ToLower = "c" Then

Console.WriteLine("Correct")

Console.WriteLine("")

Console.WriteLine("I guessed your number in " &
CStr(counter) & " tries")

Console.WriteLine("Exit Y/N")

Console.ReadLine()

End If

counter += 1

loop



End ub

 
Make the Variables outside of any subs (but inside the class). Say your guessing a number between 1-100 you's set them to those. then as the user clicks hight or lower you'd then set the min/max value. So say at the start you click higher min would then be 50 and max would be 100. You could work out the next number from these, either by finding the mid (75) or by using a random number between 50-100.

Hope that helps

TPM
 
Ah your useing a console, in that case ignore what I was saying about globals.
 
Dim min as integer = 1
Dim max as integer = 100
guess - 50
if the user selects higher min = 50
if the user selects lower max = 50
next guess is (max - min) \ 2
 
Back
Top