lots of exercises in java... from https://github.com/exercism/java

BeerSong.java 1.1KB

12345678910111213141516171819202122232425262728
  1. public class BeerSong {
  2. public String verse(int number) {
  3. switch (number) {
  4. case 0:
  5. return "No more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.\n\n";
  6. case 1:
  7. return "1 bottle of beer on the wall, 1 bottle of beer.\nTake it down and pass it around, no more bottles of beer on the wall.\n\n";
  8. case 2:
  9. return "2 bottles of beer on the wall, 2 bottles of beer.\nTake one down and pass it around, 1 bottle of beer on the wall.\n\n";
  10. default:
  11. return String.format("%d bottles of beer on the wall, %d bottles of beer.\nTake one down and pass it around, %d bottles of beer on the wall.\n\n", number, number, number - 1);
  12. }
  13. }
  14. public String sing(int start, int stop) {
  15. StringBuilder songOutput = new StringBuilder();
  16. for (int i=start; i>=stop; i--) {
  17. songOutput.append(verse(i));
  18. }
  19. return songOutput.toString();
  20. }
  21. public String singSong() {
  22. return sing(99,0);
  23. }
  24. }