• Post Reply Bookmark Topic Watch Topic
  • New Topic

can anyone please explain why these codes work?  RSS feed

 
Caoson Nguyen
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was going through the Head First java book and stumbled on this exercise which gave me an output as a-b c-d and asked to put together the broken pieces of codes. I tried to write my own first without looking at the given codes and somehow came up with these:



The codes do not really make sense to me but it generates the same output as given. Also, the broken pieces of codes after put together (or what i think they are) are:



And those do not really make sense to me either. Can anyone please help?
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I added code tags to your post (always use the tags), which make your code look so much better but the second code block shows what happens if you omit indentation
Please tell us the page number in HFJ, and whether this is some sort of puzzle. There are always multiple ways to solve computing problems, but the puzzles in HFJ make you solve them using only certain bits of code. I think you will understand the flow of control better if you write down the values of all your variables on paper and follow the execution with a pencil. Show us what you have; I don't have the time to go through all that code now.
Also consider searching this forum. HFJ is a popular book, and many peope have solved that puzzle, so maybe there will be a discussion of that selfsame problem.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!