Z epaa
Z epaa

Reputation: 1

Making user input new number at start of a loop. VB6

So this is my code, it's a game against a computer. ?The starting number, and the the number that the game is currently are displayed in textboxes. The user can select a number from 1-3 from a dropdown, to minus from the current number. The person who gets to zero first is the loser. I'm quite new to looping, so I'm unsure as to which one to use in this case. At the moment, the code just continues to loop, as I do not know how a new number can be input at the start of the loop... Any help will be greatly appreciated :)

    Randomize()
    StartingNumber = Int((30 - 20 + 1) * Rnd()) + 20
    TextBox1.Text = StartingNumber


     'Start loop

        UserNumber = ComboBox1.Text
        CurrentNumber = StartingNumber - UserNumber
        TextBox2.Text = CurrentNumber
        If UserNumber = "" Then
            MsgBox("Starting Number: " + StartingNumber)
        Else
            MsgBox("You chose: " + UserNumber + vbCrLf + "The new number is: " + CurrentNumber)
        End If


        Randomize()
        ComputerNumber = Int((Rnd() * 3) + 1)
        CurrentNumber = CurrentNumber - ComputerNumber
        MsgBox("The computer chose: " + ComputerNumber + vbCrLf + "The new number is: " + CurrentNumber)
        TextBox2.Text = CurrentNumber

     'Loop until current number = 0

    If CurrentNumber - UserNumber = "0" Then
        MsgBox("You lost, the computer won!")
    ElseIf CurrentNumber - ComputerNumber = "0" Then
        MsgBox("You won!")

    End If

End Sub

Upvotes: 0

Views: 54

Answers (1)

Hrqls
Hrqls

Reputation: 2951

you dont need a loop in your code.

the steps you need to do are:

init

  • set starting number

when user choses the number from the combobox

  • substract the number from the total
  • test if the result is 0 : user won, reinit
  • calculate computer choice
  • substract computer number from total
  • test if the result is : computer won, reinit

Upvotes: 1

Related Questions