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:10
Info:http://java.sun.com
Score: (3.00 in 27 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
object-oriented versionAnonymous04/20/0514
bytecode-version with loaderTilo Dickopp05/23/069
Java 5.0 object-oriented versionKvols11/19/053
exception orientedJarek Ratajski09/08/051
Singing with Java Speech APIKevin Seifert05/04/061

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";);
}
}
}

>>  Mike said on 06/08/07 17:30:15

Mike public Class BeerSong {
public static void main(String[] args) {
String word = "bottles";
for(int bottles = 99; bottles > 0; --bottles) {

if(bottles == 1) {
String word = "bottle";
}

System.out.println(bottles + word + " of beer on the wall, " + bottles + word + " of beer.";);
System.out.println("Take one down and pass it around, " + bottles + word + " of beer.";);
}

System.out.println("No more bottles of beer on the wall, no more bottles of beer.";);
System.out.println("Go to the store and buys some more, 99 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);}}

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: