Language Java
(standard version)
Date: | 04/20/05 |
Author: | Sean Russell |
URL: | n/a |
Comments: | 12 |
Info: | http://java.sun.com |
Score: | ![]() |
<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
Version | Author | Date | Comments | Rate |
---|---|---|---|---|
object-oriented version | Anonymous | 04/20/05 | 33 | ![]() ![]() |
exception oriented | Jarek Ratajski | 09/08/05 | 5 | ![]() ![]() |
bytecode-version with loader | Tilo Dickopp | 05/23/06 | 10 | ![]() ![]() |
Java 5.0 object-oriented version | Kvols | 11/19/05 | 3 | ![]() ![]() |
Singing with Java Speech API | Kevin Seifert | 05/04/06 | 2 | ![]() ![]() |
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!
Comments
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.
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"
}
}
}
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"
}
}
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."
}
}
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"
}
}
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"
}
}
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"
's')+e)+w);}}
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.
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!"
);
}
}
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 "
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).