Java Reference
In-Depth Information
+ " of " + stuff + ";");
System.out.println("You take one down "
+ "and pass it around:");
--n;
plural = (n == 1) ? "" : "s";
if (n == 0)
break loop;
System.out.println(n + " bottle" + plural
+ " of " + stuff + " on the wall!");
System.out.println();
}
System.out.println("No bottles of " +
stuff + " on the wall!");
}
public static void main(String[] args) {
printSong("slime", 3);
}
}
the method printSong will print a version of a children's song. Popular values for stuff
include “ pop ” and “ beer ”; the most popular value for n is 100 . Here is the output that
results from running the program:
Click here to view code image
3 bottles of slime on the wall,
3 bottles of slime;
You take one down and pass it around:
2 bottles of slime on the wall!
2 bottles of slime on the wall,
2 bottles of slime;
You take one down and pass it around:
1 bottle of slime on the wall!
1 bottle of slime on the wall,
1 bottle of slime;
You take one down and pass it around:
No bottles of slime on the wall!
In the code, note the careful conditional generation of the singular “ bottle ” when ap-
propriate rather than the plural “ bottles ”; note also how the string concatenation oper-
ator was used to break the long constant string:
"You take one down and pass it around:"
into two pieces to avoid an inconveniently long line in the source code.
Search WWH ::




Custom Search