Skip to content

Instantly share code, notes, and snippets.

@mmonge
Created July 1, 2020 07:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmonge/c90ed10575b95bf1c81b31a99684c34a to your computer and use it in GitHub Desktop.
Save mmonge/c90ed10575b95bf1c81b31a99684c34a to your computer and use it in GitHub Desktop.
import java.util.stream.IntStream;
import static java.lang.String.format;
class Sing99BottlesOfBeerOnTheWall {
private static final int TOTAL_BOTTLES = 100;
public static void main(String[] args) {
IntStream.rangeClosed(1, TOTAL_BOTTLES)
.map(i -> TOTAL_BOTTLES - i)
.forEach(current -> {
System.out.println(
format("%s of beer on the wall, %1$s of beer.",
howManyBottles(current)));
if (current > 0) {
System.out.print("take one down and pass it around, ");
} else {
System.out.print("go to the store and buy some more, ");
current = TOTAL_BOTTLES;
}
System.out.println(
format("%s of beer on the wall.\n", howManyBottles(current - 1)));
});
System.exit(0);
}
public static String howManyBottles(int bottles) {
if (bottles > 1) {
return format("%s bottles", bottles);
} else if (bottles > 0) {
return format("%s bottle", bottles);
} else {
return format("%s bottles", "no more");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment