Results 1 to 7 of 7
  1. #1
    TheWhopper is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default Help with "Head First Java" quiz

    Having a difficult time (just started learning Java a couple days ago) trying to figure out how the code below gets the following output

    Java Code:
    00 11 21 32 42
    I couldn't figure it out just by looking through the book, but I verified the output above is correct after I tested in Eclipse.

    Java Code:
    class Test {
      public static void main(Strings [] args) {
       int x = 0;
       int y = 0; 
       while ( x < 5 ) {
          y = x - y;
          System.out.print(x + "" + y +" ");
          x = x + 1;
       }
      }
    }
    Here is the actual page in the book "Head First Java"

    Head first Java - Google Books

    Any ideas how the code above produces this output?

    Java Code:
    00 11 21 32 42

    Many thanks!!
    Last edited by TheWhopper; 06-04-2011 at 04:27 AM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    9

    Default

    What exactly dont you understand?(assuming the u in the print is meant to be a y). Why not try tracing the loop one step at a time. Find out what x and y are before you reach the print statement by hand.

  3. #3
    TheWhopper is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default

    (replaced the 'u' with a 'y') I'm mainly stuck due to the quotations. I have a decent grasp of the 'while loop', it's just that part I don't know enough of. If you can give me some input regarding the quotations please....
    Last edited by TheWhopper; 06-04-2011 at 04:41 AM.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    9

    Default

    The quotes are used to convert the ints to strings, once they are hanged to strings all the +'s are overloaded to be string concatenation, the blank quotes(""), just makes the numbers appear next to eachother, the spaced quotes(" ") add a space between the first number pair and the second.

  5. #5
    TheWhopper is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default

    Thanks sunde887!! I just learned several things (some fell into place) just by the information you provided!

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    9

    Default

    Your welcome, glad I helped you out.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,821
    Rep Power
    20

    Default

    Read about the specifics of the String concatenation operator here:
    Expressions

    db

Similar Threads

  1. Replies: 14
    Last Post: 04-08-2011, 06:01 PM
  2. Help: " no pulse-java" problem - Head First Java
    By kmckinley820 in forum Java Applets
    Replies: 2
    Last Post: 02-25-2011, 08:13 PM
  3. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 04:03 AM
  4. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 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
  •