Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language Java

(standard version)

Date:04/20/05
Author:Sean Russell
URL:n/a
Comments:12
Info:http://java.sun.com
Score: (3.02 in 49 votes)
<A HREF=http://java.sun.com>Java</A> is a machine independent 
compiler based on C++ which targets to pseudo-code.

// java version of 99 bottles of beer on the wall
// 1995 Sean Russell (ser@cs.uoregon.edu)

class bottles
{
  public static void main(String args[])
    {
    String s = "s";
    for (int beers=99; beers>-1;)
      {
      System.out.print(beers + " bottle" + s + " of beer on the wall, ");
      System.out.println(beers + " bottle" + s + " of beer, ");
      if (beers==0)
        {
        System.out.print("Go to the store, buy some more, ");
        System.out.println("99 bottles of beer on the wall.\n");
        System.exit(0);
        }
      else
        System.out.print("Take one down, pass it around, ");
      s = (--beers == 1)?"":"s";
      System.out.println(beers + " bottle" + s + " of beer on the wall.\n");
      }
    }
}

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
bytecode-version with loaderTilo Dickopp05/23/0610
exception orientedJarek Ratajski09/08/055
object-oriented versionAnonymous04/20/0533
Singing with Java Speech APIKevin Seifert05/04/062
Java 5.0 object-oriented versionKvols11/19/053

Comments

>>  John said on 10/27/06 15:40:46

John Too inefficient for a simple standard version.

Better one would be...

public class Beer
{
public static void main(String args[])
{
String s=" ";
for(int x=99; x>0; x--)
{
System.out.println(x+" bottles of beer on the wall "+x+" bottles of beer";
System.out.println("Take one down, pass it around, "+(x-1)+" bottles of beer on the wall.\n;
}
System.out.print("Go to the store, buy some more, ";);
System.out.println("99 bottles of beer on the wall.\n";);
System.exit(0);
}
}

Since there is no need for the if statement, given the fact that it will exit the for loop and reach there afterwards anyway.

>>  asdasfd said on 11/02/06 21:15:06

asdasfd ^^^I hate you!!!^^^

>>  rune said on 11/10/06 18:14:35

rune John: While Sean's code is not perfect it is at least able to output the correct lyrics...

>>  @xel said on 01/29/07 03:39:49

@xel The simplest and most straight-forward version I can imagine (as close to the original in the "history" as possible):

public class Beer {
public static void main(String[] args) {
for (int i = 99; i > 0; i--) {
System.out.println(i + " bottle(s) of beer on the wall, " + i + " bottle(s) of beer";);
System.out.println("Take one down and pass it around, " + (i - 1) + " bottle(s) of beer on the wall\n";);
}
}
}


The most performant version I can imagine (again as close to the original in the "history" as possible):

public class Beer {
public static void main(String[] args) {
for (int i = 99; i > 0;) {
System.out.println(i + " bottles of beer on the wall, " + i + " bottles of beer\n" + "Take one down and pass it around, " + --i + " bottles of beer on the wall";);
}
}
}

>>  AlmSiVi said on 08/27/07 01:59:43

AlmSiVi public class Bottles {

public static void main(String[] args) {

String word = "bottles";
for (int bottles = 99; bottles > 0;) {
System.out.println( bottles + " " + word + " of beer on the wall, " + bottles + " " + word + " of beer.";);
bottles = bottles - 1;
if (bottles == 1) word = "bottle";
if (bottles == 0)
System.out.println("Take one down and pass it around, no more bottles of beer on the wall.\n";);
else
System.out.println("Take one down and pass it around, " + bottles + " " + word + " of beer on the wall.\n";);
}
System.out.println("No more bottles of beer on the wall, no more bottles of beer.";);
System.out.println("Go to the store and buy some more, 99 bottles of beer on the wall.\n";);

}

}

>>  Human Way to write this said on 10/03/07 21:36:03

Human Way to write this class beer {

static void throwBeer(int i) {
String s1 = "s", s2 = "s", nm = String.valueOf(i-1);
if (i==2) s2 = "";
if (i==1) {
s1 = "";
nm = "no more";
}
System.out.println(i+" bottle"+s1+" of beer on the wall, "+i+" bottle"+s1+" of beer.";);
System.out.println("Take one down and pass it around, "+nm+" bottle"+s2+" of beer on the wall.\n";);
}

public static void main(String[] args) {
for (int i = 99; i >= 1; i--) throwBeer(i);
System.out.println("No more bottles of beer on the wall, no more bottles of beer.";);
System.out.println("Go to the store and buy some more, 99 bottles of beer on the wall.";);
}

}

>>  aalku said on 02/13/08 01:30:02

aalku /** 'format' style: */
public class Bottles {
static final String p1 = "%1$s bottle%2$s of beer";
static final String p2 = " on the wall";
static final String p3 = "Take one down and pass it around, ";
static final String p4 = "Go to the store and buy some more, ";

static final String patterns[] = { p1 + p2 + ", " + p1 + ".%n",
p3 + p1 + p2 + ".%n", p4 + p1 + ".%n" };

public static String plural(int count) {
return count != 1 ? "s" : "";
}

public static void main(String[] args) {
for (int bottleCount = 99; bottleCount > 0; bottleCount--) {
for (int j = 0; j <= 1; j++) {
System.out.format(patterns[j], (bottleCount - j),
plural(bottleCount - j));
}
}
System.out.format(patterns[0].replaceFirst("%1\\$s", "No more";),
"no more", "s";);
System.out.format(patterns[2], 99, "s";);
}
}

>>  Tom 101 said on 02/22/08 23:13:34

Tom 101 public class Beer {
public static void main(String[] args) {
System.out.println("99 bottles of beer on the wall, 99 bottles of beer";);
for (int i = 99; i > 2;) {
System.out.println("Take one down and pass it around, " + --i + " bottles of beer on the wall";);
System.out.println(i + " bottles of beer on the wall, " + i + " bottles of beer";);
}
System.out.println("Take one down and pass it around, 1 bottle of beer on the wall";);
System.out.println("1 bottle of beer on the wall, 1 bottle of beer\n" + "Take one down and pass it around, no more bottles of beer on the wall";);
System.out.println("No more bottles of beer on the wall, no more bottles of beer\n" + "Go to the store and buy some more, 99 bottles of beer on the wall\n";);
}
}

>>  Rune Berge said on 05/17/08 01:38:25

Rune Berge Here's a really minimalistic version:

class B{public static void main(String[]a){int n=99;String o=" bottle",e=
" of beer",b=n+o+'s'+e,w=" on the wall\n";while(n>0)System.out.println(b+
w+b+"\nTake one down, and pass it around\n"+(b=(--n>0?n:"No";)+o+(n==1?"":
's')+e)+w);}}

>>  Helga said on 03/14/09 19:38:39

Helga Hi guys. The toughest question has always been, "How do you get your ideas?" How do you answer that? It's like asking runners how they run, or singers how they sing. They just do it!
I am from Central and now teach English, give true I wrote the following sentence: "Around the world airfare, complex international airline tickets, itineraries for global trips."

With best wishes :D, Helga.

>>  Jon said on 08/28/09 07:05:19

Jon here's a simple version using my own style :D

public class BottlesOfBeer
{
public static void main
(
String[] args
)
{
int k = 99;
while
(
k != 0
)
{
System.out.printf
(
"%d bottle%s of beer on the wall,\n" +
"%d bottle%s of beer,\n" +
"take one down,\n" +
"pass it around,\n" +
"%d bottle%s of beer on the wall...\n"
k,
((k != 1) ? "s" : "";),
k,
((k != 1) ? "s" : "";),
--k
((k != 1) ? "s" : "";),
);
}
System.out.print
(
"No more bottles of beer on the wall!"
);
}
}

>>  Adam Parkin said on 09/03/09 21:55:16

Adam Parkin When I think of a straightforward version, I did this (just the method, leaving out the class declaration stuff for brevity):

public static String bottlesOfBeerLyrics (int NUMBOTTLES)
{
String retVal = "";

for (int x = NUMBOTTLES; x > 1; x--)
retVal += (x + " bottles of beer on the wall, " + x +
" bottles of beer.\nTake one down and pass it around, " + (x - 1) +
((x - 1) > 1 ? " bottles " : " bottle ";) + "of beer on the wall\n\n";);

retVal += "1 bottle of beer on the wall, 1 bottle of beer.\n";
retVal += "Take one down and pass it around, no more bottles of beer on the wall.\n\n";
retVal += "No more bottles of beer on the wall, no more bottles of beer.\n";
retVal += "Go to the store and buy some more, " + NUMBOTTLES + " bottles of beer on the wall.\n";

return retVal;
}

There is a slight inefficiency in the sense of the conditional being evaluated upon each iteration, but if efficiency is a concern you'd just do the lyrics as one big string literal. Or, if you insist on having the generation be parametrized you could move the x == 2 case outside of the loop (like I did with the x == 1 case).

Download Source | Write Comment

Add Comment

Please provide a value for the fields Name, Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.

Please don't post large portions of code here! Use the form to submit new examples or updates instead!

Name:

eMail:

URL:

Security Code:
  
Comment: