Results 1 to 6 of 6
  1. #1
    Honijahu is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Got exercise, right.... But don't know why...Can you help?

    Hi, I'm doing an exercise from Head first java, and they wanted me to put pieces of code together to get this output: a-b c-d.

    I proceeded to write this as the code:

    public class Classhuffle {

    public static void main(String[] args) {

    int x = 3;
    while (x > 0) {
    if (x > 2) {
    System.out.print("a");
    }
    x = x - 1;
    System.out.print("-");
    if (x == 2) {
    System.out.print("b c");
    }
    if (x == 1) {
    System.out.print("d");
    x = x - 1;
    }
    }
    }
    }

    The code is correct I got the right answer. But here is my question. I understand why there is a minus sign between the a and the b. I had put in System.out.print("-");
    But what I don't get is that seeing I typed this command out only once. Why is there a minus sign between the c and the b. Obviously I'm missing something. Anybody can tell me what that is?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: Got exercise, right.... But don't know why...Can you help?

    Did you try tracking what your algorithm does by hand? The second minus sign is printed when your loop executes a second time; I find it a tricky little thingy ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    946
    Rep Power
    2

    Default Re: Got exercise, right.... But don't know why...Can you help?

    First of all, put your code in [code][/code] tags to keep formatting.

    What helps is when you grab pen and paper, and write down x when the loop runs, and what is being printed to screen.

  4. #4
    Honijahu is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Re: Got exercise, right.... But don't know why...Can you help?

    Sorry verry newbian question here, what do you mean with tags to keep formatting?

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,235
    Rep Power
    19

    Default Re: Got exercise, right.... But don't know why...Can you help?

    Quote Originally Posted by Honijahu View Post
    Sorry verry newbian question here, what do you mean with tags to keep formatting?
    Guide For New Members
    BB Code List - Java Programming Forum - Learn Java Programming

    Also go through the Forum Rules, particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Honijahu is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Re: Got exercise, right.... But don't know why...Can you help?

    I got the answer. Thanks everybody for their suggestions.

    By the time x gets to the line:
    Java Code:
    if (x == 2) {System.out.print("b c");}
    x is equal to two.

    But because after that there is no line stating:
    Java Code:
    x = x - 1;
    it cannot execute the next section:
    Java Code:
    if (x == 1) {System.out.print("d");
    so it loops back up.
    It cannot execute the
    Java Code:
    if (x > 2) {System.out.print("a");}
    section either because x is still 2.
    But it can execute
    Java Code:
    x = x - 1;System.out.print("-");
    so x now being equal to 1 it executes the last section.

    Problem solved thanx again for the tips.

Similar Threads

  1. Have I done this exercise right?
    By ccie007 in forum New To Java
    Replies: 7
    Last Post: 09-28-2010, 05:54 PM
  2. Exercise for java 3d
    By armiri in forum Java 2D
    Replies: 2
    Last Post: 05-13-2010, 11:14 PM
  3. I/O exercise
    By Feldom in forum New To Java
    Replies: 1
    Last Post: 10-28-2007, 04:48 PM
  4. help with exercise
    By e_as're in forum New To Java
    Replies: 3
    Last Post: 09-25-2007, 10:14 AM
  5. help with an exercise calcuting tax
    By e_as're in forum New To Java
    Replies: 7
    Last Post: 08-01-2007, 03:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •