473,399 Members | 3,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

VB Program Help

Hi I was wondering if anyone could help me with this homework problem of mine? I need to create a program that does this:

Write a program that plays “guess the number” as follows: Your program chooses the number to be guessed by selecting an Integer at random in the range 1–1000. The program then displays the following text in a label:

I have a number between 1 and 1000--can you guess my number?
Please enter your first guess.

A TextBox should be used to input the guess. As each guess is input, the background color should change to red or blue. Red indicates that the user is getting “warmer,” blue that the user is getting “colder.” A Label should display either “Too High” or “Too Low,” to help the user “zero-in” on the correct answer. When the user guesses the correct answer, display “Correct!” in a message box, change the form’s background color to green and disable the TextBox. Provide a Button that allows the user to play the game again. When the Button is clicked, generate a new random number, change the background to the default color and enable the TextBox.

Anyones help would be greatly appreciated...
Jul 11 '07 #1
8 2744
kadghar
1,295 Expert 1GB
I think it would be better if you give it a try by yourself first and then ask here some questions about particular troubles you had while making your program.

If you have no idea, I recomend you to make the whole code into the Command Button click's sub. remember you're working with an object orientated language, dim an integer to generate the number (this should be done outside the command button since you dont want to re-generate it each time the user makes a guess).

Hope that helps (and if it doesnt, take a quick view to the VB help archive to learn the properties and methods and how to use their sintaxis).
Jul 11 '07 #2
Killer42
8,435 Expert 8TB
The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.

Please read the Posting Guidelines and particularly the Coursework Posting Guidelines.

Then when you are ready post a new question in this thread.

MODERATOR
Jul 12 '07 #3
The main problem I am having is setting up the number generator. I know it goes something like this.

Dim randomobject As Random = New Random()
Dim nrand As Integer = 0


nrand = randomobject.next(1,1000)

but what else do I need to start?
Jul 12 '07 #4
Here is what I got so far... I am lost though. First off I do not know if I declared everything correctly? Also how do I get the guess to input after it is typed in the box? And how do you configure the warm/cold colors? Please help!

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.    Dim randomobject As Random = New Random()
  3.    Dim nrand As Integer = randomobject.Next(1, 1001)
  4.  
  5.    Dim guess As Integer
  6.  
  7.  
  8.  
  9.    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
  10.       guess = TextBox1.Text
  11.  
  12.  
  13.  
  14.    End Sub
  15.  
  16.    Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
  17.       If guess < nrand Then
  18.          Label2.Text = ("Too Low")
  19.          If guess > nrand Then
  20.             Label2.Text = ("Too High")
  21.          Else : MsgBox("Correct")
  22.          End If
  23.       End If
  24.    End Sub
  25. End Class
Jul 12 '07 #5
kadghar
1,295 Expert 1GB
The main problem I am having is setting up the number generator. I know it goes something like this.

Dim randomobject As Random = New Random()
Dim nrand As Integer = 0


nrand = randomobject.next(1,1000)

but what else do I need to start?
its easier than that:

dim nrand as integer

nrand = int(rnd * 1000)+1

... and that's it
Jul 12 '07 #6
Killer42
8,435 Expert 8TB
nrand = int(rnd * 1000)+1
Are you sure that syntax still works in VB.Net?

By the way, if you have a look in the VB Articles area, we have a couple of items there on the subject of generating pseudo-random numbers. (The computer can't generate truly random numbers.)
Jul 12 '07 #7
kadghar
1,295 Expert 1GB
Are you sure that syntax still works in VB.Net?

By the way, if you have a look in the VB Articles area, we have a couple of items there on the subject of generating pseudo-random numbers. (The computer can't generate truly random numbers.)
i didnt see the class... my apologies. :(
Jul 12 '07 #8
Here is what I have so far. I am stuck on getting a play again button to reset the game back to normal and how to get the game to compare the previous guess to the new guess and differenciate between warm or cold. I know i need to use the math.abs() function. Can anyone help.



Public Class Form1
Dim randomobject As Random = New Random()
Dim nrand As Integer = randomobject.Next(1, 1001)

Dim guess As Integer
Dim previousguess As String = guess










Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If guess < nrand Then
Label3.Text = "Too Low"
End If


If guess > nrand Then
Label3.Text = "Too High"

End If

If guess = nrand Then
MsgBox("Correct!")
Me.BackColor = Color.Green
TextBox1.Enabled = False
End If


End Sub

Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click



End Sub


Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
guess = TextBox1.Text

End Sub
End Class
Jul 14 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: anuradha.k.r | last post by:
hi, i am writing a socket program in python,both client side and server side.I've written the client side which is working perfectly fine(checked it against server program written in C).but as for...
2
by: stanlo | last post by:
Hallo to everyone, i am just begining to learn c++ even though i did pascal when i studied mathematics in the univesity.i just took on my self a project which writing a c++ program which does...
7
by: tyler_durden | last post by:
thanks a lot for all your help..I'm really appreciated... with all the help I've been getting in forums I've been able to continue my program and it's almost done, but I'm having a big problem that...
1
by: Willing 2 Learn | last post by:
Below is a program I did to recognize a Finite State Automata for ASCII (J+H)*. I got that one working but im having trouble getting the NFA program to work. I really desperately need help! My...
66
by: genestarwing | last post by:
QUESTION: Write a program that opens and read a text file and records how many times each word occurs in the file. Use a binary search tree modified to store both a word and the number of times it...
12
by: asif929 | last post by:
I am trying to write a program which creates four triangles. The program begins with prompting a user " Enter the size of triangles", number from 1 to N is the size of four triangles For Example if...
21
by: asif929 | last post by:
I need immediate help in writing a function program. I have to write a program in functions and use array to store them. I am not familiar with functions and i tried to create it but i fails to...
0
by: ashishbathini | last post by:
Hi guys here is my problem ... this is the source code I Have , honestly I hav no idea how it works bcos its too complicated for me .... but my problem is ... i hav a freq comonent in it .......
9
by: C#_Help_needed | last post by:
I need help with the following question. THANKS :) Write a program in c# that takes in a directory as a command line parameter, and returns the longest repeated phrase in ALL text files in that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.