Click here to Skip to main content
15,881,455 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Message box is shown and when I click OK it appears again with the same response "Player guess is smaller than Computer" or "Player guess is bigger than computer". I must use Do While or Do until loops as this is for a school assignment.


VB
Public Class Form1
    Dim RandomNumber As New Random
    Dim ComputerNumber As Integer
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ComputerNumber = RandomNumber.Next(1, 100)
    End Sub
    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        ComputerNumber = RandomNumber.Next(1, 100)
    End Sub

    Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheck.Click
        Dim PlayerGuess As Integer
        Dim Attempt As Integer
        PlayerGuess = txtPlayerGuess.Text
        Do Until (PlayerGuess = ComputerNumber)
            If PlayerGuess < ComputerNumber Then
                MsgBox("Player number Smaller than computer")
                Attempt = Attempt + 1
            ElseIf PlayerGuess > ComputerNumber Then
                MsgBox("Player number Bigger than computer")
                Attempt = Attempt + 1
            Else
                MsgBox("Congratulations")
            End If
        Loop
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub


End Class
Posted
Comments
Wombaticus 16-Nov-14 16:29pm    
Well of course it will - there is no player input in your Do...Loop

1 solution

The problem is that you are entering the do...while when clicking object, but you are not leaving it at all, because you are not giving the possibility to change the number and click another time. In other words, what you have is evaluating only the very first number you input and then remains in the loop forever.

I am not sure if it is a must, that you are using a form and clicks but...

As example I am going to give you the pseudocode of a console application

main
   do
      print "give me a number, to end the game type [exit]"
      get the input

      if (bigger)
         print "number is bigger"
      else if (smaller)
         print "number is smaller"
      else
         print "that is the number"
    
    while ((input not equal to number) or (input not equal to "exit")
end


do you see the difference? once you get into the loop you have to give a chance to correct that number or get out of the loop.

In your example, a possibility (not elegant but feasible) would be to use non-modal messagebox, so the user can type another number and then click ok.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900