求助找错,我给的答案。和题目要求一样啊,为什么BlueJ上 Unit Text 不能通过呢?

cycchina 2008-04-08 11:05:30
/**
* 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;
}

}
...全文
42 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧