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

BeerSong.java 1.1KB

12345678910111213141516171819202122232425262728293031
  1. public class BeerSong {
  2. public static String verse(int number)
  3. {
  4. switch (number)
  5. {
  6. case 0:
  7. 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";
  8. case 1:
  9. 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";
  10. case 2:
  11. 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";
  12. default:
  13. 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);
  14. }
  15. }
  16. public static String sing(int start, int stop)
  17. {
  18. StringBuilder songOutput = new StringBuilder();
  19. for (int i=start; i>=stop; i--) {
  20. songOutput.append(verse(i));
  21. }
  22. return songOutput.toString();
  23. }
  24. public static String singSong() {
  25. return sing(99,0);
  26. }
  27. }