Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Another Question from the book "Head First Java"

 
andrew dale
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • 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?

 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
 
Daniel Vlad
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use int x=3 as it was and try it like this:


 
Rob Spoor
Sheriff
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Hmm using NetBeans, you're code says build successful but, outprints nothing?
 
James Hughes
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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:



 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic