求助找错,我给的答案。和题目要求一样啊,为什么BlueJ上 Unit Text 不能通过呢?
/**
* Print out all verses of the song "n Green Bottles" for a given start
e * value n. All verses are the same except for the count of the number of
* bottles (and the number minus 1). The output in the case of n == 3 should be:
3 green bottles hanging on the wall
3 green bottles hanging on the wall
And if 1 green bottle should accidentally fall
There'll be 2 green bottles hanging on the wall
2 green bottles hanging on the wall
2 green bottles hanging on the wall
And if 1 green bottle should accidentally fall
There'll be 1 green bottle hanging on the wall
1 green bottle hanging on the wall
1 green bottle hanging on the wall
And if 1 green bottle should accidentally fall
There'll be no green bottles hanging on the wall
Note that there must be a blank line between verses but no
extra blank line at the end or the start.
Note the following tricky aspects:
(a) The word "bottles" becomes just "bottle" when there's only 1!
(b) The case of "0 green bottles" is written as "no green bottles".
*/
public static void nGreenBottles(int numberOfBottles)
{
String b="";
String c="";
for(int n=numberOfBottles;n>0&&n<=numberOfBottles;n--)
{
if(n==1)
b="bottle";
else
b="bottles";
if((n-1)<1)
{
c="And if 1 green bottle should accidentally fall\nThere'll be no green bottles hanging on the wall";
}
else
{
if(n==2)
c="And if 1 green bottle should accidentally fall\nThere'll be "+(n-1)+" green bottle hanging on the wall";
else
c="And if 1 green bottle should accidentally fall\nThere'll be "+(n-1)+" green "+b+" hanging on the wall";
}
System.out.println(numberOfBottles+" green "+b+" hanging on the wall");
System.out.println(numberOfBottles+" green "+b+" hanging on the wall");
System.out.println(c);
System.out.println();
numberOfBottles-=1;
}
}