Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: My code is working correctly, but I don't know why it is working correctly...

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry My code is working correctly, but I don't know why it is working correctly...

    Hello all,

    I am new to forum so first of all I would like to introduce myself. My name is Nick and I work for a software company. I supersize a team of software testers. I am not a coder myself, but have learned a lot in the last couple years of testing code. I am currently taking a intro to java class and we are now doing projects on loops. The project I am doing now is to get a sum of all of the off numbers in the loop depending on the input a user enters.

    So I know how to setup the part where the users input a variable and am familiar with the try and catch section for exceptions...

    This is my code so far without those sections included and just uses a predetermined value for my limit.

    public class demoLoops2
    {
    public static void main(String[] args)
    {
    int counter=0;
    int sum=0;

    for (counter = 1; counter <=10; counter +=2) sum = sum + counter;// Loop with incrementor
    {

    System.out.println("This is the sum of the odd numbers in the range " + sum);

    }//End of the multi statement for loop

    The sum ends up being correct at 25 which would be 1+3+5+7+9=25. My question is how did my variable sum go from 0 to 25? The line of code I wrote : sum = sum + counter; to me means sum (0) = sum (0) + counter (which at that point I'm sure now what the counter value is. Is the counter value 9 because 9 is the last integer it can get to before it sets the loop false? In that case it would be 0 = 0 + 9... I'm so confused as to how I am arriving at the correct answer....

    Can someone please help explain this to me.

    Thanks,

    Nick


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: My code is working correctly, but I don't know why it is working correctly...

    Welcome, Nick!
    Please read this topic for newbies here: Announcements - What's Wrong With My Code?

    To answer your question:
    sum doesn't stay 0. In your loop it is:
    1) sum = 0+1 -> 1
    2) sum = 1+3 -> 4
    3) sum = 4+5 -> 9
    etc.

Similar Threads

  1. My text input file is not working correctly?
    By MLeclerc182 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 17th, 2012, 08:34 PM
  2. My code is not working correctly
    By shavek911 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 18th, 2012, 05:49 PM
  3. [SOLVED] For Loop not working correctly.
    By chrisob in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 15th, 2012, 03:50 PM
  4. Classpath override not working correctly in command prompt?
    By MeteoricDragon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 20th, 2011, 12:05 PM
  5. Hey guys, my While loop isn't working correctly.
    By metanoia in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 26th, 2011, 11:15 PM