This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes need help figuring this out. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "need help figuring this out." Watch "need help figuring this out." New topic
Author

need help figuring this out.

pat kat
Greenhorn

Joined: Jul 11, 2008
Posts: 2
this is 99 bottles of beer program..it is just looping the same thing 99 times..what can i do to fix this?? please help


<blockquote>code:
<pre name="code" class="java">
public class BeerSong {



private int bottles = 99;

public BeerSong(int number)
{
if ((number < 0)||(number > 100))
System.exit(0);
}



public void printNumInEnglish(int n)
{
int tens = bottles/10;
int ones = bottles%10;
String t = new String();
String o = new String();


switch (tens)
{
case 0:
t = "Zero";
break;
case 1:
switch (ones)
{
case 0:
o = "Ten";
break;
case 1:
o = "Eleven";
break;
case 2:
o = "Twelve";
break;
case 3:
o = "Thirteen";
break;
case 4:
o = "Fourteen";
break;
case 5:
o = "Fifteen";
break;
case 6:
o = "Sixteen";
break;
case 7:
o = "Seventeen";
break;
case 8:
o = "Eighteen";
break;
case 9:
o = "Nineteen";
break;
}
case 2:
t = "Twenty";
break;
case 3:
t = "Thirty";
break;
case 4:
t = "Forty";
break;
case 5:
t = "Fifty";
break;
case 6:
t = "Sixty";
break;
case 7:
t = "Seventy";
break;
case 8:
t = "Eighty";
break;
case 9:
t = "Ninety";
break;
}
if (tens != 1)
{
switch (ones)
{
case 1:
o = "One";
break;
case 2:
o = "Two";
break;
case 3:
o = "Three";
break;
case 4:
o = "Four";
break;
case 5:
o = "Five";
break;
case 6:
o = "Six";
break;
case 7:
o = "Seven";
break;
case 8:
o = "Eight";
break;
case 9:
o = "Nine";
break;
}

}

System.out.print(t + " " + o +" ");



}
/**
* Outputs an entire stanza for n bottles.
*/
public void printStanza(int n) {
// output n in English
printNumInEnglish(n);

// account for "one bottle" vs. many "bottles"
if (n == 1) {
System.out.println("bottle of beer on the wall, ");
}
else {
System.out.println("bottles of beer on the wall, ");
}

printNumInEnglish(n);
if (n == 1) {
System.out.println("bottle of beer, ");
}
else {
System.out.println("bottles of beer, ");
}
System.out.println("Take one down, pass it around,");
n--;

printNumInEnglish(n);
if (n == 1) {
System.out.println("bottle of beer on the wall.");
}
else {
System.out.println("bottles of beer on the wall.");
}
System.out.println();
}

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(23);
bs.printSong();
}

}

</pre>
</blockquote>
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Here's a hint... the printNumInEnglish method takes an int named 'n' which is the number of bottles for the current verse... where does that value ever get used?
[ July 11, 2008: Message edited by: Mark Vedder ]
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5812
    
    7

And another hint. In printNumInEnglish, what variable is being used to determine the current number of bottles of beer on the wall?


JBoss In Action
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18538
    
  40

Yet another hint. When you pass a value to the constructor, is that value saved anywhere?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
pat kat
Greenhorn

Joined: Jul 11, 2008
Posts: 2
why is the program starting from 60 bottles of beer?

<blockquote>code:
<pre name="code" class="java">

public class BeerSong {


int bottles = 99;



public BeerSong(int somenumber)
{
if ((somenumber < 0)||(somenumber > 100))
System.exit(0);
}



public void printNumInEnglish(int bottles)

{
int tens = bottles/10;
int ones = bottles%10;
String t = new String();
String o = new String();


switch (tens)
{
case 0:
t = "Zero";
break;


case 1:
switch (ones)

{
case 0:
o = "Ten";
break;
case 1:
o = "Eleven";
break;
case 2:
o = "Twelve";
break;
case 3:
o = "Thirteen";
break;
case 4:
o = "Fourteen";
break;
case 5:
o = "Fifteen";
break;
case 6:
o = "Sixteen";
break;
case 7:
o = "Seventeen";
break;
case 8:
o = "Eighteen";
break;
case 9:
o = "Nineteen";
break;


}
System.out.print(t + "" + o +" ");

case 2:
t = "Twenty";
break;
case 3:
t = "Thirty";
break;
case 4:
t = "Forty";
break;
case 5:
t = "Fifty";
break;
case 6:
t = "Sixty";
break;
case 7:
t = "Seventy";
break;
case 8:
t = "Eighty";
break;
case 9:
t = "Ninety";
break;
}

if (tens != 1)
{
switch (ones)
{
case 1:
o = "One";
break;
case 2:
o = "Two";
break;
case 3:
o = "Three";
break;
case 4:
o = "Four";
break;
case 5:
o = "Five";
break;
case 6:
o = "Six";
break;
case 7:
o = "Seven";
break;
case 8:
o = "Eight";
break;
case 9:
o = "Nine";
break;
}
System.out.print(t + " " + o +" ");
}





}
/**
* Outputs an entire stanza for n bottles.
*/
public void printStanza(int n) {
// output n in English
printNumInEnglish(bottles);

// account for "one bottle" vs. many "bottles"
if (n == 1) {
System.out.println("bottle of beer on the wall, ");
}
else {
System.out.println("bottles of beer on the wall, ");
}

printNumInEnglish(bottles);
if (n == 1) {
System.out.println("bottle of beer, ");
}
else {
System.out.println("bottles of beer, ");
}
System.out.println("Take one down, pass it around,");
bottles--;

printNumInEnglish(bottles);
if (n == 1) {
System.out.println("bottle of beer on the wall.");
}
else {
System.out.println("bottles of beer on the wall.");
}
System.out.println();
}

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(23);
bs.printSong();
}

}


</pre>
</blockquote>
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
Welcome to JavaRanch
It printed ninety nine for me . . .
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18538
    
  40

Originally posted by Campbell Ritchie:
Welcome to JavaRanch
It printed ninety nine for me . . .



Yeah, it starts from Ninety Nine for me as well. I am guessing you want it to start from Twenty Three, to which my answer is ... take a look at the hint that I provided earlier.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need help figuring this out.
 
Similar Threads
Beersong
Head first Beersong
Beer Song
head first beersong problem?
BeerSong Java code