• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Another Question from the book "Head First Java"

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Second question - Second post ---Sorry guys, I'm addicted to Java I wish I would've found out about this earlier in my life!


The Question in the book is called "Code Magnents" it gives you line of code in mixed up boxes, and your goal is to put them in the right way.

The answer when the code works is "a-b c-d"

My code does that but I had to change to Which I am pretty sure you're not supposed to do.

Anyways heres the full code, can you spot what I did wrong?

 
Ranch Hand
Posts: 258
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your code gives "~a~b c~d" where you said you have got "a-b c-d"

Rearrange the following statement may give you different result
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use int x=3 as it was and try it like this:


 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or remove those 3 separate x = x - 1 statements and add one single x = x - 1 statement as the last statement of the loop, just after the third if-block.
 
Daniel Vlad
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:Or remove those 3 separate x = x - 1 statements and add one single x = x - 1 statement as the last statement of the loop, just after the third if-block.



You posted right as I noticed that mistake. Sorry about that, Rob is right.
I had those x = x - 1 statements around because I was messing with the code, trying to do it in more than just one way, and i forgot to comment them out when i moved on to the other chapters.

I also edited my previous post(the actual code) to the way it was supposed to be when using the code magnets from the book.
 
andrew dale
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm using NetBeans, you're code says build successful but, outprints nothing?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I don't know how to post printscreens into this forum, but I came up with this arrangement which seemed to work:



 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

We don't like screenshots, but we do like code tags which I have added (and removed some excess blank lines) and doesn't it look a lot better.
By the way: nobody seems to have said which page that exercise is on.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic