99 bottles of beer: Difference between revisions
(Ada example) |
m (→GUI: Should have used libheaders) |
||
Line 57: | Line 57: | ||
===GUI=== |
===GUI=== |
||
{{ |
{{libheader|Swing}} |
||
{{ |
{{libheader|AWT}} |
||
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. |
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; |
<java>import java.awt.BorderLayout; |
Revision as of 05:26, 29 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/
Ada
<ada>with Ada.Text_Io; use Ada.Text_Io;
procedure Bottles is begin for X in reverse 1..99 loop Put_Line(Integer'Image(X) & " bottles of beer on the wall"); Put_Line(Integer'Image(X) & " bottles of beer"); Put_Line("Take one down, pass it around"); Put_Line(Integer'Image(X - 1) & " bottles of beer on the wall"); New_Line; end loop; end Bottles;</ada>
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>