• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Visual Basic noob needs help!

Ok, so End of semester is gettung near and I have to finish my last VB prject, and I am truly screwed on it. First of all, let's note that my skill in VB is sub-par, to put it kindly. So I have about a week to finish making a "magic numbers" game (e.g. guessing random numbers form 1-10 generated randomly). You get 3 guesses at the number. The app must keep track of how many games you win and how many games your computer wins. After 3 rounds, the computer should display the score between you and the app. My teacher has been away, and the sub is a bit over her head, so I figured I'd ask here. I'll paste the code that I have so far here, hopefully you guys can give me some advice, or direct me to a good Visual Basic help forum. Thanks in advance.

Private Sub Form_Load()
Dim games, guess, UserGuess, CompNum, userscore, compscore As Integer
For games = 1 To 3
CompNum = Int((10 - 1 + 1) * Rnd + 1)
Do
UserGuess = InputBox("Guess a number between 1 and 10", "Guessing Game")
If UserGuess > CompNum Then MsgBox " Your guess is too high! "
If UserGuess < CompNum Then MsgBox " Your guess is too low! "
If UserGuess = CompNum Then MsgBox " Correct! "
If UserGuess = CompNum Then userscore = userscore + 1
End If

Loop Until UserGuess = CompNum Or guess = 3

Next
End Sub
 
What's the point of "10 - 1 + 1" when you're generating the random numbers? Why not just Int(10 * Rnd + 1)?

Also, can you just post a more specific question? Exactly what do you need help with?
 
You are not initializing guess and not incrementing guess in your Do loop. So, what you are seeing is that it will keep asking until you guess the number correctly and will then go to the next round. It is doing just what you told it to do. Also, the End If looks like a syntax error. You meant to test "Userguess=compnum then" do stuff with an end if. Instead, you retested the condition and have a have a useless end if.
 
Private Sub Form_Load()
Dim games, guess, UserGuess, CompNum, userscore, compscore As Integer
For games = 1 To 3
CompNum = Int((10 - 1 + 1) * Rnd + 1)

Do
UserGuess = InputBox("Guess a number between 1 and 10", "Guessing Game")

If UserGuess > CompNum Then
Guess +=1
MsgBox " Your guess is too high! "
end if
If UserGuess < CompNum Then
Guess +=1
MsgBox " Your guess is too low! "
end if
If UserGuess = CompNum Then
Guess +=1
MsgBox " Correct! "
end if
If UserGuess = CompNum Then
userscore = userscore + 1
end if

Loop Until UserGuess = CompNum Or guess = 3
guess = 0
Next games
msgbox("score is " + userscore)
End Sub
 
Back
Top