记录一下,这个思路太赞啦!
原网址
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.
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.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.
What HAS to happen first? The first line is “a noise” so I know I need to use an ‘a’ at minimum.
I will leave the while loop as undefined for now. I don’t know how many times it will run, yet.
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’.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?
We need a ‘space’ and then “noise” to be printed.
In the if test I will include the space since that HAS to happen next.
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.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?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.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).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.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!Done with the first round of the while loop.
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?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.The ‘a’ and ‘n’ were provided (everytime) so adding this last “noys” finished the output of “annoys”
We move to the bottom of the while loop, printing a new line and increment x by 1.
Done with round 2 of the while loop.
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).
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.Look, in the pool there is System.out.print(" oyster")! Everything we need, so let’s add it!
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?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!Done!