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 4 of 4

Thread: Please explain to me what I am doing wrong?

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Please explain to me what I am doing wrong?

    Hello!
    I am preparing for my exam. And I am doing some java exercises from the book. But here I have some problem. My answer does not match with the book's answer. Please explain what I am doing wrong.

    here is the code:


    public class EX5 {
    public static void main (String [ ] args) {
    int [ ] data = {6, 2, 5, 1, 10, 3, -3};
    int result = 0;
    for (int i = 2; i < data.length; i++) {
    result = result + (data[i] - data[i-2]);
    System.out.println("result = " + result);
    }
    }
    }
    the book answer is
    result = -1
    result = -2
    result = 3
    result = 5
    result = -8
    .

    But my answer is :
    -1
    -1
    5
    2
    -13

    Best regards


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,387
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: Please explain to me what I am doing wrong?

    When posting code, please use the highlight tags to preserve formatting.

    I copied that code and got the same as the book. Are you sure you're recompiling and running the correct class file?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jul 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please explain to me what I am doing wrong?

    Ok next time i'll post codes' as yo suggested.
    Well, the book says that I have to give answer by myself without using computer.


    I calculated by myself.
    First I thought that " i " starts from 2. so result + data[2] - data [2-2]= 0+ 5 - 6 = -1
    then i increasing by 1, right (i++)
    result + data [3] - data [3-2]= 0 + 1 - 2 = -1
    result + data [4] - data [4-2]= 0 + 10 - 5 = 5 and so on.

    which is obviously wrong answer. I want to know what i am doing wrong though

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,387
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: Please explain to me what I am doing wrong?

    Ah, I see, you're trying to run through the code in your head.

    Result does not rest to 0 each time through the loop. I recommend stepping through this with a debugger if you aren't sure what's going on.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. [SOLVED] Can someone explain to me?
    By unleashed-my-freedom in forum Java Theory & Questions
    Replies: 5
    Last Post: July 3rd, 2012, 04:10 AM
  2. Can Some one explain to me why the output will be like this...
    By lunchong in forum What's Wrong With My Code?
    Replies: 7
    Last Post: December 31st, 2011, 11:19 PM
  3. i need an explain please !
    By keep smiling in forum Java Theory & Questions
    Replies: 3
    Last Post: December 21st, 2011, 10:22 AM
  4. Replies: 1
    Last Post: December 13th, 2010, 04:13 AM
  5. can anyone explain this?
    By chronoz13 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 12th, 2009, 02:51 AM