Howard wrote:
Hello everyone (total VB.NET beginner here),
I'm reading the "SAMS Teach Yourself VB.NET In 21 Days" book, and came
across an exercise that I can't get to work. The exercise asks that
you create a game that makes the user guess a number from 1-100, and
you tell the user "lower" or "higher" as they input their guesses,
until they guess the correct number, at which point you then tell the
user "Correct". I tried using the While Loop, and it "sort of" worked,
but after a while (no pun intended) of inputting numbers (guesses), it
said "Correct" to incorrect answers.
<snip>
and then sent some code:
<snip> Sub Main()
Console.WriteLine("Pick a number from 1 to 100")
Dim StrInput As Integer = Console.ReadLine()
While (StrInput < "27")
Console.WriteLine("higher")
StrInput = Console.ReadLine()
End While
While (StrInput > "27")
Console.WriteLine("lower")
StrInput = Console.ReadLine()
End While
Console.WriteLine("Correct!")
StrInput = Console.ReadLine()
End Sub
<snip>
The While loop will execute until a given condition becomes true. Its
structure is:
While Condition
'Do something
End While
where Condition represents a boolean expression.
In your case, the condition that "governs" the loop is the number
entered being diferent from a given value: Number <> Value.
Therefore, your While loop would look like this:
While Number <> Value
'Do something
End While
If the condition ceases to be true then the looping will stop and
execution will proceed at the next instruction.
In your case this will happen only when Number ceases to be different
from Value. In other words, when execution breaks out of the loop, we
know we have a Number which is equal to Value and therefore we may do
something about it, which is to congratulate the user and finish the
program:
While Number <> Value
'Do something
End While
Console.WriteLine("Correct!")
Now, back to the While loop. What to do while Number remains different
from Value? The problem states that we must print "lower" if Number is
above Value, or "higher" if Number is bellow Value:
While Number <> Value
If Number < Value Then
Console.WriteLine("Higher")
Else
'the number can only be above the value
Console.WriteLine("Lower")
End If
End While
Console.WriteLine("Correct!")
Of course, there are still a few parts missing. Notice that a common
use-pattern of the While loop goes like this:
Get some input
While the input is not valid
Show some message
Get the input again
(end While)
Deal with the valid input
In your case, this might translate to the following actions:
'Get some input
Console.WriteLine("Pick a number from 1 to 100")
Number = Integer.Parse(Console.ReadLine())
'While the Input is not valid
While Number <> Value
'Show some message
If Number < Value Then
Console.WriteLine("Higher")
Else
Console.WriteLine("Lower")
End If
'Get the input again
Number = Integer.Parse(Console.ReadLine())
End While
'Deal with the valid input
Console.WriteLine("Correct!")
Of course, in a correct VB.Net app we need to define variable types and
constants. Therefore, the full program would be:
Sub Main()
Const Value As Integer = 27
Dim Number As Integer
Console.WriteLine("Pick a number from 1 to 100")
Number = Integer.Parse(Console.ReadLine())
While Number <> Value
If Number < Value Then
Console.WriteLine("Higher")
Else
Console.WriteLine("Lower")
End If
Number = Integer.Parse(Console.ReadLine())
End While
Console.WriteLine("Correct!")
End Sub
HTH.
Regards,
Branco.