99 bottles of beer: Difference between revisions

→‎{{header|Java}}: consistent formatting, cleanup
(→‎{{header|Java}}: consistent formatting, cleanup)
Line 3,819:
MessageFormat's choice operator is used to properly format plurals.
<lang java>import java.text.MessageFormat;
 
public class Beer
public class Beer {
{
static String bottles(final int n) {
return MessageFormat.format("{0,choice,0#No more bottles|1#One bottle|2#{0} bottles} of beer", n);
{
}
return MessageFormat.format("{0,choice,0#No more bottles|1#One bottle|2#{0} bottles} of beer", n);
 
}
public static void main(final String[] args) {
String bottles = bottles(99);
{
for (int n = 99; n > 0; ) {
String byob = bottles(99);
System.out.println(bottles + " on the wall");
for (int x = 99; x > 0;)
System.out.println(bottles);
{
System.out.println(byob"Take +one "down, onpass theit wallaround");
bottles = bottles(--n);
System.out.println(byob);
System.out.println("Takebottles one+ down," passon itthe aroundwall");
System.out.println();
byob = bottles(--x);
}
System.out.println(byob + " on the wall\n");
}
}
}</lang>
 
'''Optimized for speed and few I/O operations'''
<lang java>public class Beer {
public static void main(String[] args) {
{
int bottles = 99;
public static void main(final String[] args)
StringBuilder sb = new StringBuilder();
{
String verse1 = " bottles of beer on the wall\n";
int beer = 99;
String verse2 = " bottles of beer.\nTake one down, pass it around,\n";
StringBuilder sb = new StringBuilder();
String data[] = new String[] {verse3 = "Better bottlesgo ofto beerthe onstore theand buy some wall\nmore.",;
" bottles of beer.\nTake one down, pass it around,\n",
"Better go to the store and buy some more." };
 
while (beerbottles > 0)
sb.append(beerbottles).append(data[0]verse1).append(beerbottles).append(data[1]verse2).append(--beerbottles).append(data[0]verse1).append("\n");
 
System.out.println(sb.append(data[2]).toString(verse3));
}
}
}</lang>
 
'''Recursive'''
<lang java>public class Beer {
public static void main(String args[]) {
{
song(99);
public static void main(String args[])
}
{
 
song(99);
public static void song(int bottles) {
}
if (bottles >= 0) {
if (bottles > 1)
public static void song(int b)
System.out.println(bottles + " bottles of beer on the wall\n" + bottles + " bottles of beer\nTake one down, pass it around\n" + (bottles - 1) + " bottles of beer on the wall.\n");
{
else if (bottles == 1)
if(b>=0)
System.out.println(bottles + " bottle of beer on the wall\n" + bottles + " bottle of beer\nTake one down, pass it around\n" + (bottles - 1) + " bottles of beer on the wall.\n");
{
else
if(b>1)
System.out.println(bbottles + " bottles of beer on the wall\n" +b bottles + " bottles of beer\nTakenBetter onego down,to passthe itstore around\n"+(b-1)+"and bottlesbuy ofsome beer on the wall.\nmore!");
song(bottles - 1);
else if(b==1)
}
System.out.println(b+" bottle of beer on the wall\n"+b+" bottle of beer\nTake one down, pass it around\n"+(b-1)+" bottles of beer on the wall.\n");
}
else
System.out.println(b+" bottles of beer on the wall\n"+b+" bottles of beer\nBetter go to the store and buy some more!");
song(b-1);
}
}
}</lang>
 
Line 3,891 ⟶ 3,884:
<lang 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();//build and show the GUI
}
 
public class Beer extends JFrame {
public Beer(){
private int x= 99;
private JTextArea text;
take= new JButton("Take one down, pass it around");
 
text= new JTextArea(4,30);//size the area to 4 lines, 30 chars each
public static void main(String[] args) {
text.setText(x + " bottles of beer on the wall\n" + x + " bottles of beer");
new Beer().setVisible(true);
text.setEditable(false);//so they can't change the text after it's displayed
}
take.addActionListener(this);//listen to the button
 
setLayout(new BorderLayout());//handle placement of components
public Beer() {
add(text, BorderLayout.CENTER);//put the text area in the largest section
x = 99;
add(take, BorderLayout.SOUTH);//put the button underneath it
pack();//auto-size the window
JButton take = new JButton("Take one down, pass it around");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//exit on "X" (I hate System.exit...)
take.addActionListener(this::onTakeClick);
setVisible(true);//show it
}
text = new JTextArea(4, 30);
text.setText(x + " bottles of beer on the wall\n" + x + " bottles of beer");
text.setEditable(false);
setLayout(new BorderLayout());
add(text, BorderLayout.CENTER);
add(take, BorderLayout.PAGE_END);
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
 
publicprivate void actionPerformedonTakeClick(ActionEvent arg0event) {
JOptionPane.showMessageDialog(null, --x + " bottles of beer on the wall");
if(arg0.getSource() == take){//if they clicked the button
text.setText(x + " bottles of beer on the wall\n" + JOptionPane.showMessageDialog(null, --x + " bottles of beer on the wall");//show a popup message
if (x == 0) {
text.setText(x + " bottles of beer on the wall\n" + x + " bottles of beer");//change the text
}dispose();
if(x == 0){//if it's the end
dispose();//end
}
}
}
}</lang>