hey
im develeping this little game where you are asked to guess a number bewteen 0 and 50. it raises some wuestions for me, i have tryed some coding, but they fail all the time so im asking you for help.
1. how do i declare the random number the user is soposed to gues i have tryed: "dim guessn as double = <50" but it returns "end of statemaent expected" whats that im missing
and how do i make a life counter "dim life as double = 5"
2. lets imagin i actually got it working with random number thing, how do i refer to it
lets say the user pushes the gues button:
"if gusstxt.text = guessn then
msgbox ("grats, you did it")
else life - 1
end if"
it jsut returns: "name life/guessn not declared"
whats that
please help me, im kind of desperet
//Martin N. J.

help declaring a number and refering to it later
genius15
The example was just to show general idea not to actually create you game. However you simply need to put you variables as class level variables and move you random number generateor to the form load. There is another variable which will count the incorrect number of times you attempt to guess.
Public Class Form1
Private i As Integer
Private IncorrectCount As Integer = 0
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Val(TextBox1.Text) = i Then
MsgBox("Correct Guess")
Else
IncorrectCount = IncorrectCount + 1
MsgBox("Wrong Guess" & IncorrectCount.ToString & " time")
End If
End Sub
Private Function GetRandomInteger(ByVal MinValue As Integer, ByVal MaxValue As Integer) As Integer
Dim randomNumber As Integer
Randomize()
randomNumber = CInt(Int((MaxValue - MinValue + 1) * Rnd() + MinValue))
Return randomNumber
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
i = GetRandomInteger(1, 3)
End Sub
End Class
M Roy
think you misunderstood me..
the game will have to find the number when the form opens, or when a new game is started..
as you made it, the only have one guess, and it finds a new number everytime the push the button.
also, how can i manage the lifes
//Martin
metamata
Generating a random number
Some suggestions
Dim objRandom As New System.Random(CType(System.DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer))
Public Function GetRandomNumber(Optional ByVal Low As Integer = 1, Optional ByVal High As Integer = 100) As Integer
' Returns a random number,
' between the optional Low and High parameters
Return objRandom.Next(Low, High + 1)
End Function
Or
Private Function GetRandomInteger(ByVal MinValue As Integer, ByVal MaxValue As Integer) As Integer
Dim randomNumber As Integer
Randomize()
randomNumber = CInt(Int((MaxValue - MinValue + 1) * Rnd() + MinValue))
Return randomNumber
End Function
Both these functions return variables. So you'll need to define some variables and code something. In this case in the button click I have done a statement dim i as integer = GetRandomInteger(1, 3) which defines a variable i which I can refer to in the later line If Val(TextBox1.Text) = i Then
Example
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer = GetRandomInteger(1, 3)
If Val(TextBox1.Text) = i Then
MsgBox("Correct Guess")
Else
MsgBox("Wrong Guess")
End If
End Sub
Private Function GetRandomInteger(ByVal MinValue As Integer, ByVal MaxValue As Integer) As Integer
Dim randomNumber As Integer
Randomize()
randomNumber = CInt(Int((MaxValue - MinValue + 1) * Rnd() + MinValue))
Return randomNumber
End Function
End Class