Results 1 to 5 of 5

Thread: [2005] Pls help me

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2009
    Posts
    2

    [2005] Pls help me

    I have this teacher that doesn't offer any help and gives things that are not in the book. Even though my problem is simple I am also a beginner at this.
    I'm having trouble with a loop the accumulates odd numbers but excludes the numbers input into the to input text boxes.

    Say I put 1 in the lesser text box and 6 in the greater text box. How would I go about accumulating all odd numbers between 1 and 6. They would be 3 and 5 leaving me with a sum of 8 but im getting 9 because i can't exclude the inputs.

    My code is

    Code:
     Private Sub calcBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles calcBtn.Click
            Dim SumInteger As Integer = 0
            Dim lesserInteger As Integer = 0
            Dim greaterInteger As Integer = 100
            With Me
                lesserInteger = Integer.Parse(.lesserTB.Text)
                greaterInteger = Integer.Parse(.greaterTB.Text)
    
    
                Do While lesserInteger <= greaterInteger
                    SumInteger += lesserInteger
                    lesserInteger += 2
    
                Loop
                .answerlbl.Text = SumInteger.ToString("N")
            End With
        End Sub

  2. #2
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: [2005] Pls help me

    Code:
    Private Sub calcBtn_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs _
    ) Handles calcBtn.Click
        Dim total As Integer = 0
        Dim start As Integer = Integer.Parse(lesserTB.Text)
        Dim endnum As Integer = Integer.Parse(greaterTB.Text)
      
        If start mod 2 = 0 Then start -= 1
        
        Do While start < endnum - 2
            start += 2
            total += start
        Loop 
    
        MessageBox.Show(total.ToString())
    End Sub

  3. #3
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: [2005] Pls help me

    Quote Originally Posted by thenhemadecuic
    I have this teacher that doesn't offer any help and gives things that are not in the book.
    ...
    hehe..
    I think he his doing a much bigger help to you in the long run. Every teacher teaches what is in the textbooks, but only a few go above it to take pains to get you something more.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2009
    Posts
    2

    Re: [2005] Pls help me

    Thanks that worked better then what i had figured out

  5. #5
    New Member
    Join Date
    Feb 2009
    Posts
    3

    Re: [2005] Pls help me

    Hi,

    You may simply wish to try something like this...

    vb Code:
    1. Private Sub calcBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles calcBtn.Click
    2.         Dim SumInteger As Integer = 0
    3.         Dim lesserInteger As Integer = 0
    4.         Dim greaterInteger As Integer = 0
    5.  
    6.         With Me
    7.             lesserInteger = Integer.Parse(.lesserTB.Text) + 1
    8.             greaterInteger = Integer.Parse(.greaterTB.Text) - 1
    9.  
    10.             For ctr As Integer = lesserInteger To greaterInteger Step 1
    11.                         if ctr Mod 2 != 0 then
    12.                                     sum = sum + ctr
    13.                         End If
    14.             Next
    15.         End With
    16. End Sub
    Rahil Parikh

    Live life like there is no tomorrow!!!

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