99 bottles of beer: Difference between revisions
(Added Java, needed to spearate the verses in BASIC version) |
(→{{header|Java}}: Addd GUI version) |
||
Line 33: | Line 33: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
===Console=== |
|||
<java>public class Beer{ |
<java>public class Beer{ |
||
public static void main(String[] args){ |
public static void main(String[] args){ |
||
Line 38: | Line 39: | ||
System.out.println(x+"bottles of beer on the wall\n"+x+"bottles of beer\n"+"Take one down, pass it around\n"+(x-1)+"bottles of beer on the wall\n"); |
System.out.println(x+"bottles of beer on the wall\n"+x+"bottles of beer\n"+"Take one down, pass it around\n"+(x-1)+"bottles of beer on the wall\n"); |
||
} |
} |
||
}</java> |
|||
===GUI=== |
|||
{{works with|Swing}} |
|||
This version requires user interaction. The first two lines are shown in a text area on a window. The third line is shown on a button which you need to click to see the fourth line in a message box. The numbers update and the process repeats until "0 bottles of beer on the wall" is shown in a message box, when the program ends. |
|||
<java>import java.awt.BorderLayout; |
|||
import java.awt.event.ActionEvent; |
|||
import java.awt.event.ActionListener; |
|||
import javax.swing.JButton; |
|||
import javax.swing.JFrame; |
|||
import javax.swing.JOptionPane; |
|||
import javax.swing.JTextArea; |
|||
public class Beer extends JFrame implements ActionListener{ |
|||
private int x; |
|||
private JButton take; |
|||
private JTextArea text; |
|||
public static void main(String[] args){ |
|||
new Beer(); |
|||
} |
|||
public Beer(){ |
|||
x= 99; |
|||
take= new JButton("Take one down, pass it around"); |
|||
text= new JTextArea(4,30); |
|||
text.setText(x + " bottles of beer on the wall\n" + x + " bottles of beer"); |
|||
take.addActionListener(this); |
|||
setLayout(new BorderLayout()); |
|||
add(text, BorderLayout.CENTER); |
|||
add(take, BorderLayout.SOUTH); |
|||
pack(); |
|||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
|||
setVisible(true); |
|||
} |
|||
public void actionPerformed(ActionEvent arg0){ |
|||
if(arg0.getSource() == take){ |
|||
--x; |
|||
JOptionPane.showMessageDialog(null, x + " bottles of beer on the wall"); |
|||
text.setText(x + " bottles of beer on the wall\n" + x + " bottles of beer"); |
|||
} |
|||
if(x == 0){ |
|||
dispose(); |
|||
} |
|||
} |
|||
}</java> |
}</java> |
Revision as of 20:24, 28 February 2008
In this puzzle, print out the entire "99 bottles of beer on the wall" song. For those who do not know the song, the lyrics follow this form:
X bottles of beer on the wall X bottles of beer Take one down, pass it around X-1 bottles of beer on the wall X-1 bottles of beer on the wall ... Take one down, pass it around 0 bottles of beer on the wall
Where X and X-1 are replaced by numbers of course. Grammatical support for "1 bottle of beer" is optional. As with any puzzle, try to do it in as creative/concise/comical a way as possible (simple, obvious solutions allowed, too).
See also: http://99-bottles-of-beer.net/
BASIC
<qbasic>FOR x = 99 TO 1 STEP -1
PRINT x; "bottles of beer on the wall" PRINT x; "bottles of beer" PRINT "Take one down, pass it around" PRINT x-1; "bottles of beer on the wall" PRINT
NEXT x</qbasic>
C++
<c>#include<stdio> using namespace std; int main(int argc, char* argv[]){
for(int x = 99;x>=1; --x){ cout<<x<<"bottles of beer on the wall\n"<<x<<"bottles of beer\n"<<"Take one down, pass it around\n"<<x-1<<"bottles of beer on the wall\n\n"; }
}</c>
Java
Console
<java>public class Beer{
public static void main(String[] args){ for(int x = 99;x>=1; --x) System.out.println(x+"bottles of beer on the wall\n"+x+"bottles of beer\n"+"Take one down, pass it around\n"+(x-1)+"bottles of beer on the wall\n"); }
}</java>
GUI
This version requires user interaction. The first two lines are shown in a text area on a window. The third line is shown on a button which you need to click to see the fourth line in a message box. The numbers update and the process repeats until "0 bottles of beer on the wall" is shown in a message box, when the program ends. <java>import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JTextArea; public class Beer extends JFrame implements ActionListener{ private int x; private JButton take; private JTextArea text; public static void main(String[] args){ new Beer(); }
public Beer(){ x= 99; take= new JButton("Take one down, pass it around"); text= new JTextArea(4,30); text.setText(x + " bottles of beer on the wall\n" + x + " bottles of beer"); take.addActionListener(this); setLayout(new BorderLayout()); add(text, BorderLayout.CENTER); add(take, BorderLayout.SOUTH); pack(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); }
public void actionPerformed(ActionEvent arg0){ if(arg0.getSource() == take){ --x; JOptionPane.showMessageDialog(null, x + " bottles of beer on the wall"); text.setText(x + " bottles of beer on the wall\n" + x + " bottles of beer"); } if(x == 0){ dispose(); } } }</java>