我想知道如何定义一个函数xGreenBottles
,当给定输入x时,它将生成并返回给定xGreenBottles值的歌曲的单词,例如:从x绿色瓶子开始,每次使用循环去除1个绿色瓶子。
到目前为止我的代码
public class TenGreenBottles
{
public static void main(String[] args)
{
int bottles = 10;
while(bottles>1)
{
System.out.println(+ bottles + " green bottles standing on the wall");
System.out.println(+ bottles + " green bottles standing on the wall");
System.out.println("and if 1 green bottle should accidently fall, ");
System.out.println("there'll be " + (bottles-1) + " green bottles standing on the wall.");
System.out.println(" ");
bottles = bottles - 1;
}
System.out.println(+ bottles + " green bottle, standing on the wall,");
System.out.println(+ bottles + " green bottle, standing on the wall,");
System.out.println("and if 1 green bottle, should accidently fall,");
System.out.println("there'll be no green bottles, standing on the wall");
System.out.println(" ");
}
}
答案 0 :(得分:1)
制作这样的函数:
public void xGreenBottles(int bottles) {
// Your code from main minus your line "int bottles = 10;""
}
调用函数
xGreenBottles(10);
在你的主要方法中做你想做的事。