So, this may be a pretty basic question, but I've scoured my textbook and have tried experiments, and am just frustrated! I'm working on the pretty common 99 bottles of beer program, and while it compiles and runs practically perfectly, I just can't figure out how to return my numbers as text.
public class BeerSong {
/**
* Number of bottles that we start out with
*/
private int bottles = 0;
public BeerSong(int n)
{
bottles = n;
}
public void printStanza (int n)
{
String [] numbers = { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
String [] tens = { "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
if (n < 20)
System.out.println(numbers[n] + " bottles of beer on the wall, \n" + numbers[n] + " bottles of beer,\n" + "Take one down, \n" + "Pass it around, \n" + numbers[n-1] + " bottles of beer on the wall.\n");
else
if (n % 10 == 0)
System.out.println(tens[(n/10)-2] + " bottles of beer on the wall \n" + n + " bottles of beer,\n" + "Take one down, \n" + "Pass it around, \n" + " bottles of beer on the wall.\n");
else
System.out.println(tens[(n/10)-2] + " " + numbers[n%10] + " bottles of beer on the wall \n" + n + " bottles of beer,\n" + "Take one down, \n" + "Pass it around, \n" + (n-1) + " bottles of beer on the wall.\n");
}
public void printSong() {
// Loop from 99 down to 0
for (int num = bottles; num > 0; num--) {
printStanza(num);
}
}
public static void main(String[] args) {
BeerSong bs = new BeerSong(99);
bs.printSong();
}
}
In the first
if statement, it works great (for numbers 1-19). However, when I try to use tens as I did
numbers[n]
in the first
if, I get errors when the program runs. No compile errors, however. If anyone can help give me any kind of direction, that would just be fantastic! Thank you so much!