Head First Java 2nd - chapter1 - PoolPuzzleOne

记录一下,这个思路太赞啦!
原网址

It really is up to you to find the solution to this puzzle. There are many ways you can go about doing it and most of the time for these Pool Puzzles you will make a mistake and have to rewrite some of the code. I did try the puzzle and I’ll post a summation of my thought process.

  1. I studied the output. How could I make the output come out in the format it was presented by using the code in the pool? All the
    outputs are specified as print() and not println(). Very good to know
    that you have to rely on the open line puzzle code for making new
    lines in the output.

  2. int x = 0. x starts at zero so I know that whatever condition I make in the code needs to pass for x’s value.

  3. What HAS to happen first? The first line is “a noise” so I know I need to use an ‘a’ at minimum.

  4. I will leave the while loop as undefined for now. I don’t know how many times it will run, yet.

  5. The first line is blank, I know that ‘a’ must be printed first so I use system.out.print(“a”); for the first empty line. Great, now it
    will print ‘a’.

  6. The if test tests for (x < 1). x is less than 1 right now (it’s 0) so this if block will run. What needs to happen next?

  7. We need a ‘space’ and then “noise” to be printed.

  8. In the if test I will include the space since that HAS to happen next.

  9. The next line is tricky. I could put in something like System.out.print(“noise”) and have “noise” printed. But this statement
    would be outside of an if block so it will print EVERYTIME. I can’t
    have “noise” printed every single time. What do the output lines have
    in common that they could have printed everytime? An ‘n’. Therefore, I
    add System.out.print(“n”); to the line.

  10. Now we have ‘a’ ‘space’ ‘n’ printed, so we just need ‘oise’. There is a statement for “oise” it is System.out.print(“oise”); But where
    does it go?

  11. The remaining if blocks… One has two empty lines, another has to be equal to one so that won’t work and the last has one empty line.
    That might work… Also, below the System.out.println(""); there is an
    empty line? If I put it there that means “oise” would print everytime.
    Can’t have that! So it would best fit in the last if block.

  12. Now that I put System.out.print(“oise”) into the last if block, we need to make sure it passes its boolean test. x is equal to what right
    now, x is equal to 0. The best suited test to insert would be if( x <
    1).

  13. What needs to happen next? I need a new line, no code in the pool will provide a new line so I need the System.out.println(""); at the
    bottom.

  14. There is still a problem though, x is still equal to 0. Don’t we need to increment x? Yup! So on that last empty line let’s throw in an
    increment statement. Let’s try x = x +1. That should work! Now we are
    at the bottom of the first round of the while loop!

  15. Done with the first round of the while loop.

  16. So what needs to happen next? We need “annoys” to print. I know that everytime I will have an ‘a’ printed and an ‘n’ printed so I know
    that I can’t simply find a spot for system.out.print(“annoys”); I have
    to build “annoys” from an ‘a’ and an ‘n’. What is left is “noys”
    Anything in the pool for noys? Yup. I see System.out.print(“noys”); So
    I have to find a spot to add this, but where?

  17. Well, x is equal to 1 this time and there is an if( x == 1) so let’s just try to fill that one empty line in the if(x==1) block and
    see if it works. So I add System.out.println(“noys”) to that if block.

  18. The ‘a’ and ‘n’ were provided (everytime) so adding this last “noys” finished the output of “annoys”

  19. We move to the bottom of the while loop, printing a new line and increment x by 1.

  20. Done with round 2 of the while loop.

  21. x is now equal to 2. And we only have one if block left. So let’s try and add to the remaining if block the test of if(x>1).

  22. Now that we can access this block, what needs to happen next? We have an ‘a’ and ‘n’ printed, so all we need is a space and oyster to
    print.

  23. Look, in the pool there is System.out.print(" oyster")! Everything we need, so let’s add it!

  24. Great everything should print. But we are still not done, there is one line left in the if block with System.out.print(" oyster"). What
    could go there?

  25. Well we still haven’t defined the while loop’s test yet so let’s kill this loop after about 4 runs, since x<4 is in the pool. So, if x
    is 2 right now, let’s kick it up by using the x = x +2 and the next
    time this loop tries to run the while loop fill return false!

  26. Done!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值