Results 1 to 11 of 11

Thread: Coding Help

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2003
    Posts
    23

    Question 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

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    here's a quick example ( in this case using values inputted to a textbox )
    VB Code:
    1. [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]
    2.             MessageBox.Show("it's an odd number!")
    3.         [color=blue]Else[/color]
    4.             MessageBox.Show("it's even")
    5.         [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]

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    If you mean EVERY even number between 1 and 100 then here is an example:
    VB Code:
    1. 'loop thru 1(2 since 1 is odd) to 100 skipping every other one (step 2)
    2.         Dim sum As Integer
    3.         Dim count As Integer
    4.         For nmbr As Integer = 2 To 100 Step 2
    5.             'increment the count
    6.             count += 1
    7.             'add to the sum
    8.             sum += nmbr
    9.         Next
    10.         'get average
    11.         Dim result As Single = sum / count
    12.         'build a string to show results
    13.         Dim sb As New System.Text.StringBuilder
    14.         sb.Append(String.Format("There a {0} even numbers between 1 and 100.{1}", count, ControlChars.NewLine))
    15.         sb.Append(String.Format("The sum of these numbers is {0}.{1}", sum, ControlChars.NewLine))
    16.         sb.Append(String.Format("The average of these numbers is {0}.", result))
    17.         'show results string
    18.         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.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Sep 2003
    Posts
    23
    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

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    If you aren't using VS 2003 then the loop will give an error and you have to change this line:
    VB Code:
    1. For nmbr As Integer = 2 To 100 Step 2
    to this:
    VB Code:
    1. Dim nmbr As Integer
    2. For nmbr = 2 To 100 Step 2

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Sep 2003
    Posts
    23
    Sorry, I didn't see all of your post. What does append do???

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Sep 2003
    Posts
    23
    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.

  8. #8
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Sep 2003
    Posts
    23

    Wink

    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

  10. #10
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    += is a mathmatically version of append these two statements mean the samething:

    x=x+y

    x+=y

    Its just a short cut.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Sep 2003
    Posts
    23

    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
  •  



Click Here to Expand Forum to Full Width