|
-
Sep 26th, 2003, 03:35 PM
#1
Thread Starter
Junior Member
Coding Help
Good Afternoon,
I am currently taking a VB.NET class and the book is really no help when it comes to coding something so I will ask here. If I were to code a command button to find the average of all even numbers between 1 and 100 (inclusive) how would I go about doing that. My guess would be it would involve making a loop so it can check each number to decide if it is even or not, but I do not even know really how to begin. Any response is greatly appreciated as I am trying to learn this.
JAZ
-
Sep 26th, 2003, 03:58 PM
#2
here's a quick example ( in this case using values inputted to a textbox )
VB Code:
[color=blue]If[/color] TextBox1.Text.EndsWith(1) [color=blue]Or[/color] TextBox1.Text.EndsWith(3) [color=blue]Or[/color] TextBox1.Text.EndsWith(5) [color=blue]Or[/color] TextBox1.Text.EndsWith(7) [color=blue]Or[/color] TextBox1.Text.EndsWith(9) [color=blue]Then[/color]
MessageBox.Show("it's an odd number!")
[color=blue]Else[/color]
MessageBox.Show("it's even")
[color=blue]End If[/color]
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Sep 26th, 2003, 04:12 PM
#3
If you mean EVERY even number between 1 and 100 then here is an example:
VB Code:
'loop thru 1(2 since 1 is odd) to 100 skipping every other one (step 2)
Dim sum As Integer
Dim count As Integer
For nmbr As Integer = 2 To 100 Step 2
'increment the count
count += 1
'add to the sum
sum += nmbr
Next
'get average
Dim result As Single = sum / count
'build a string to show results
Dim sb As New System.Text.StringBuilder
sb.Append(String.Format("There a {0} even numbers between 1 and 100.{1}", count, ControlChars.NewLine))
sb.Append(String.Format("The sum of these numbers is {0}.{1}", sum, ControlChars.NewLine))
sb.Append(String.Format("The average of these numbers is {0}.", result))
'show results string
MsgBox(sb.ToString)
If you mean a selection of numbers between 1 and 100 then build off of dynamic_sysop's post.
Last edited by Edneeis; Sep 26th, 2003 at 04:16 PM.
-
Sep 26th, 2003, 04:13 PM
#4
Thread Starter
Junior Member
First of all,
Congratulations on your Beautiful Twins!!!
Thank you for your response. How can you make the code average the numbers from 1 to 100? I think we are supposed to use a Do statement or a For statement to test with a loop. Sorry, I am not great at the terminology either.
Once again, Congratulations your children are truly a blessing!!!
JAZ
-
Sep 26th, 2003, 04:14 PM
#5
If you aren't using VS 2003 then the loop will give an error and you have to change this line:
VB Code:
For nmbr As Integer = 2 To 100 Step 2
to this:
VB Code:
Dim nmbr As Integer
For nmbr = 2 To 100 Step 2
-
Sep 26th, 2003, 04:15 PM
#6
Thread Starter
Junior Member
Sorry, I didn't see all of your post. What does append do???
-
Sep 26th, 2003, 04:17 PM
#7
Thread Starter
Junior Member
Thank you very much for both of your replies. Yes I must check every number from 1 to 100 and your posts make much more sense than my book does.
-
Sep 26th, 2003, 04:18 PM
#8
Append adds to the end, in the code I posted it adds that string to the end of the strinbuilder to be shown in the messagebox.
-
Sep 26th, 2003, 04:23 PM
#9
Thread Starter
Junior Member
One more question, please. What does it mean += as I have not seen these two together??? Just asking so I can use it in the future. Thank you very much for your time.
JAZ
-
Sep 26th, 2003, 04:30 PM
#10
+= is a mathmatically version of append these two statements mean the samething:
x=x+y
x+=y
Its just a short cut.
-
Sep 26th, 2003, 04:32 PM
#11
Thread Starter
Junior Member
Thank you
Thank you very much!!! I really appreciate your time and help!!! Have a Great Weekend!!!
JAZ
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|