Random Generator in Loop - vb.net

I am having some problems with an assignment. The case project is:
Create an application that allows the user to guess a random number generated by the computer. When the user makes an incorrect guess, the application should move an image either upr or down, depending on how the guess compares to the random number. If the random number is greater than the user's guess, the application should move the image up to indicate that the user needs to guess a higher number. If the random number is less than the user's guess, the application should move the image down to indicate that the user needs to guess a lower number. The game ends when the user guesses the random number. However, the application should allow the user to stop the game prematurely. When that happens the application should siplay the random number.
I have tried every which way I can think of including using a textbox instead of an inputbox and playing around with the syntax - but just can't seem to get it right. Advice would be much appreciated. Thanks.
My code:
Public Class Form1
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Me.Close()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Number As Integer
Dim inputNumber As Integer
Dim answer As Integer
Dim isconverted As Boolean
Dim computerchoice As New Random
answer = computerchoice.Next(1, 20)
inputNumber = InputBox("Please guess number", "Random Number Game")
Do
isconverted = Integer.TryParse(inputNumber, Number)
If isconverted = True Then
If answer = Number Then
MessageBox.Show("You Win")
ElseIf answer > Number Then
PictureBox1.SetBounds(0, 90, 0, 0, BoundsSpecified.Y)
ElseIf answer < Number Then
PictureBox1.SetBounds(0, 220, 0, 0, BoundsSpecified.Y)
End If
Else
MessageBox.Show("Please enter a valid number between 1 - 20 only")
End If
inputNumber = InputBox("Please guess number", "Random Number Game")
Loop While answer <> Number
MessageBox.Show("Answer:" & answer.ToString)
End Sub
End Class

Your code actually almost worked. A few things though:
The only thing that really didn't work was the picture moving up and down. All you have to do for that is to increment/decrement the .Top property.
Because you converted your input to a number at the beginning of the loop and not evaluating till the end, you were looping through an extra time after you got the right answer.
The number comparison after the conversion was redundant, since you know they got the number if they exit the loop.
If you're new to Visual Studio and don't know about breakpoints and other debugging, it is worth it to look into those. With these tools you can pause your code at given points in your program, look at the values variables hold, and step through your code line-by-line.
Here's the working code:
Do
If isconverted = True And Number >= 1 And Number <= 20 Then
If answer > Number Then
PictureBox1.Top -= 10
ElseIf answer < Number Then
PictureBox1.Top += 10
End If
Else
MessageBox.Show("Please enter a valid number between 1 - 20 only")
End If
inputNumber = InputBox("Please guess number", "Random Number Game")
isconverted = Integer.TryParse(inputNumber, Number)
Loop While (answer <> Number)
MessageBox.Show("You Win! The answer is " & answer.ToString)

Related

visual basic: Enter some numbers until a negative value is entered to stop the program

private Sub Command1_Click()
a = InputBox("What is the Number ?", "Input Example"," ")
If a = "-1" Then
End If
End Sub
the whole question is: Enter some numbers until a negative value is entered to stop the program(stop running the what is your number thing). then find the average of the entered numbers.
What I want to do, for now, I want to know how can I make my "a" variable accept any negative value like in the code above it stops when I enter -1 but I want to stop when I enter -3, -10, or any negative value.
There are some helpful answers down below
If you are expecting the usual input to be text then you can use the Double.TryParse method to check if a number was entered. If it was, then you can check if that number is negative, and exit the application if so:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim userMsg As String
userMsg = Microsoft.VisualBasic.InputBox("What is your message?", "Message Entry Form", "Enter your message here", 500, 700)
If userMsg <> "" Then
Dim x As Double
If Double.TryParse(userMsg, x) AndAlso x < 0 Then
Application.Exit()
End If
MessageBox.Show(userMsg)
Else
MessageBox.Show("No Message")
End If
End Sub
The AndAlso operator only looks at the second argument if the first evaluated to True.
If you would like to repeat some portion of code till specific condition is met, you need to use:
While...End While Statement (Visual Basic)
or
Do...Loop Statement (Visual Basic)
It's also possible to write conditional loop using For... Next statement
Dim myNumber As Integer = 0
Dim mySum As Integer = 0
Dim myCounter As Integer = 0
Do
Dim answer As Object = InputBox("Enter integer value", "Getting average of integer values...", "")
'check if user clicked "Cancel" button
If answer <> "" Then
'is it a number?
If Int32.TryParse(answer, myNumber)
If myNumber >=0 Then
mySum += myNumber
myCounter += 1
Else
Exit Do
End If
End If
End If
Loop
Dim average As Double = mySum/myCounter
'you can display average now
Tip: Do not use InputBox, because this "control" is VisualBasic specific. Use custom Form instead. There's tons of examples on Google!

Same number is generated in RNG in VB.NET

I am attempting to make a random number generator in VB 16 in Visual Studio, but every time I run this I keep getting 71, I've tried making it public, sharing it and several other things, but it won't work. I am trying to make a program that has the user guess a randomly generated number, and continue guessing until they get it, but for some reason the exact same number is chosen each time. It won't work properly specifically in window app forms. How do I get a random number each time?
Public Shared Randomize()
Dim value As Integer = CInt(Int((100 * Rnd()) + 1))
Public Sub EnterBtn_Click(sender As Object, e As EventArgs) Handles EnterBtn.Click
Dim entervalue As String = EnterTxt.Text
Dim chances As Integer
Select Case entervalue
Case > value
ResTxt.Text = "Too big"
chances += 1
Case < value
ResTxt.Text = "Too small"
chances += 1
Case = value
ResTxt.Text = "Well done, you got it in " & chances & " tries"
End Select
End Sub
You were close! Here's a working example modifying your original logic:
Private random As Random = New Random()
Private value As Integer
Private chances As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
value = random.Next(1, 100)
chances = 0
End Sub
Private Sub EnterBtn_Click(sender As Object, e As EventArgs) Handles EnterBtn.Click
Select Case EnterTxt.Text
Case > value
chances += 1
ResTxt.Text = "Too big"
Case < value
chances += 1
ResTxt.Text = "Too small"
Case = value
chances += 1
ResTxt.Text = "Well done, you got it in " & chances & " tries"
'and reset for next attempt
value = random.Next(1, 100)
chances = 0
End Select
End Sub
Since your code is not correct it is hard to pinpoint the problem. It is also not clear what the code is supposed to do.
Try this
Private Shared PRNG As New Random ' add this
value = PRNG.Next(1, 101)'this will set value to a random number between 1 and 100 inclusive
Here's some skeleton code for you:
Dim rnd As New Random()
For i as Integer = 0 to 10
Console.WriteLine("{0,15:N0}", rnd.Next())
Next
Notice the rnd.Next() thing. Hope it helps.

LOOP PROBLEMS in VB.Net

I am creating a math tutorial app for kids in the primary level. And I am having a hard time in creating a loop that would generate 2 random numbers,then check if the answer is right/wrong for 5 times. also, the button would be disabled after clicking 5 times. The other things are clear to me except the idea of how to put it in a loop. can someone help me please? thanks! I tried using the FOR LOOP, but sadly, it would just loop 5 times but it would only check the answer 1 time.I need it to check 5 different answers.
For ctr As Integer = 1 To 5
Button3.Enabled = False
initialize()
If TextBox3.Text = sum Then
MsgBox("correct")
point = point + 1
TextBox3.Focus()
Else
MsgBox("wrong")
MsgBox(sum)
TextBox3.Focus()
End If
Next
MsgBox(point)
If you want to process five different TextBoxes in a loop, one way to do that is to create an array containing all the TextBoxes and loop through the array.
Dim boxes() As TextBox = {TextBox3, TextBoxX, TextBox22, TextBoxBB, TextBoxA}
For Each box As TextBox in boxes
box.Focus()
If box.Text = sum Then
MsgBox.("correct")
point += 1
Else
MsgBox("wrong")
End If
Next
Alternatively, if you want the user to enter five answers one at a time in a single TextBox, you can have the user click a button each time an answer is entered. You should define the counter outside the button's click handler.
Private ctr As Integer
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
ctr += 1
If ctr > 5 Then Button3.Enabled = False
initialize()
If TextBox3.Text = sum Then
MsgBox("correct")
point += 1
Else
MsgBox("wrong")
MsgBox(sum)
End If
MsgBox(point)
End Sub

vb word counter richtextbox covering multiple lines

I am programming novel management software, and I need to be able to keep track of the amount of words in the richtextbox accurately. This is what I have so far
Dim Change As Integer
Dim Count As Integer
Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged
Change = RichTextBox1.Text.Split(" ").ToLookup(Function(x) x).Count()
'Add 1 to the variable Change every time the space bar is pressed
Count = Change
If RichTextBox1.Text = "" Then
Count = 0
'If the textbox is empty, the word count is 0
ElseIf RichTextBox1.Text.EndsWith(" ") = True Then
Count = Change - 1
'take one away from the wordcount variable when the last character is a space
End If
ToolStripStatusLabel2.Text = Count
'Display the wordcount
End Sub
How do I get the code to keep going on multiple lines? So far, the code only runs on the text on the first line. If the user hits enter then keeps typing, the the word count doesnt count the first word on each subsequent lines
You can use something like this in the keydown handler. There may be some special cases for leading and trailing spaces, unless it can be off by 1, and backspaces should be handled.
If e.KeyValue = Keys.Space Or e.KeyValue = Keys.Back Then
ss = rText1.Text.Split
txCount.Text = UBound(ss) + 1
End If

VB textbox and width prob

hi there im trying to design a section in my program that builds layers represented by rectangles, depending on the sizes inputed will decide upon the outcome of the rectangle width. Im having a problem when entering < 0 it will revert to 1 or 0.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim rectangle As Integer
rectangle = Val(TextBox1.Text)
TextBox1.Text = Convert.ToString(rectangle)
Form2.RectangleShape1.Width = Val(TextBox1.Text)
If Val(TextBox1.Text) >= 1.0 Or Val(TextBox1.Text) <= 1.5 Then
Form2.RectangleShape1.Width = 75
End If
If Val(TextBox1.Text) >= 1.5 Then
Form2.RectangleShape1.Width = 120
End If
If Val(TextBox1.Text) <= 1.0 Then
Form2.RectangleShape1.Width = 55
End If
Form2.RectangleShape1.Show()
Me.Hide()
It's hard to say with the provided information and I don't understand exactly what you are doing but a few things seems wrong there.
The you lost me part
Let's look at this first :
Dim rectangle As Integer
rectangle = Val(TextBox1.Text)
TextBox1.Text = Convert.ToString(rectangle)
Form2.RectangleShape1.Width = Val(TextBox1.Text)
So you declare an integer
Take the text of some textbox and select only the numbers from it by taking out characters
You assign back that value in the Textbox with a ToString() on the Integer
You assign the value of the textbox once again taking out characters (you already did that)
You can resume this line with :
Form2.RectangleShape1.Width = CDbl(Val(TextBox1.Text))
I changed the conversion to doubles because I'm certain you want decimals. Integers can't have decimals. Without decimals, these two conditions would be the exact same thing :
If Val(TextBox1.Text) >= 1.0 Or Val(TextBox1.Text) <= 1.5 Then
Form2.RectangleShape1.Width = 75
End If
If Val(TextBox1.Text) >= 1.5 Then
Form2.RectangleShape1.Width = 120
End If
Since you can either have 1 or 2. (So checking if it's between 1 and 1.5 OR 1.5 and 2 is pointless).
Val
You probably know this but val will return the numbers in a string. So as I asked in the comments, why don't you prevent the user from inputting something else than numbers ?
Let's say you want to have this as result : "< 0".
If you prevent letter and symbol he will have to input only "0"
If you do it like you're doing right now he will input "< 0" the val function will only return "0" so telling me you need the "<" is against your current logic.
But I want the number and the symbol...
Don't worry. If you are make sure the user will only input what you want (by putting restriction on the possible characters to input), you can catch the whole content of the textbox.
If myTextBox.Text = "< 1.5" Then
'Do something cool
Else If myTextBox.Text = "< 0" Then
'Do something cooler
Else
'Do nothing
End If