• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

need help figuring this out.  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
author
Bartender
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And another hint. In printNumInEnglish, what variable is being used to determine the current number of bottles of beer on the wall?
 
author
Sheriff
Posts: 23603
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yet another hint. When you pass a value to the constructor, is that value saved anywhere?

Henry
 
pat kat
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
Marshal
Posts: 60845
190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch
It printed ninety nine for me . . .
 
Henry Wong
author
Sheriff
Posts: 23603
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!