O clássico “99 garrafas de cerveja” começa com 59. Alguém conseguiu resolver?
[code]public class BeerSong {
public static void main (String [] args){
int beerNum = 99;
String word = “bottles”;
while (beerNum > 0) {
if (beerNum == 1) {
word = "bottle";
}
System.out.println (beerNum + " " + word + " of beer on the wall");
System.out.println (beerNum + " " + word + " of beer.");
System.out.println ("Take one down!");
System.out.println ("Pass it around.");
beerNum = beerNum - 1;
if (beerNum > 0) {
System.out.println (beerNum + " " + word + " of beer on the wall");
}else{
System.out.println ("No more bootles of beer on the wall");
}
}
}
Desculpe. Bem, uma das duvidas já foi respondida. Valeu Duduribeiro!
A outra duvida é onde está a falha? Já que no livro “Use a Cabeça Java” diz que há uma falha.
Bem quem já viu esse código sabe do que estou falando.
Olha este código que eu fiz, creio que deve está certo:
for (int b = 99; b >= 0; b--) {
System.out.println(String.format("%1$s bottle%3$s of beer on the wall, %2$s bottle%3$s of beer.\n"
+ "%4$s%5$s bottle%6$s of beer on the wall.\n", b > 0 ? b : "No more", b > 0 ? b : "no more", b > 1 || b == 0 ? "s" : "", b > 0 ? "Take one down and pass it around, " : "Go to the store and buy some more, ", b > 1 ? b - 1 : b == 1 ? "no more" : 99, b - 1 >= -1 && b - 1 != 1 ? "s" : ""));
}
A falha está quando vc está com 2 cervejas e diminui o contador para 1, no seu codigo na sequencia da execução ele imprime que tem 1 garrafas no muro, penso que aplicando o mesmo if do inicio do while você resolve isso…
if (beerNum > 0) {
if (beerNum == 1) {
word = "bottle";
}
System.out.println (beerNum + " " + word + " of beer on the wall");
}else{
System.out.println ("No more bootles of beer on the wall");
}
Se for fazer uma tradução de: “99 bottles of beer on the wall” = “99 garrafas de cerveja na parede”
“99 bottles of beer.” = “99 garrafas de cerveja.”
“Take one down!” = “Tome uma!”
Resumindo:
Me parece que o cara TANTO JOGA, QUANTO BEBE, ou BEBE e JOGA A GARRAFA NA PAREDE.
Parece também que tem que mudar a ordem que as coisas atuam.
Ex.:
Estão lá [color=red]99 garrafas de cerveja[/color] (“99 bottles of beer”);
Ele bebe (“Take one down!”);
E joga uma na parede (N° de garrafas que ainda tem jogadas na parede + “bottles of beer on the wall”);
Muito complicado já que se trata também de traduzir e há coisas que não se traduz ao [color=darkred]“pé da letra”[/color].
Se estiver errado me corrijam e se tiverem outra idéia sobre o caso postem!
Boas amigos, creio que o problema é este mais abaixo quando tem apenas 1, abaixo tem a saida com o codigo do livro:
…
3 bottles of beer on the wall 3 garrafas de cerveja na parede
3 bottles of beer on the wall 3 garrafas de cerveja na parede
3 bottles of beer. 3 garrafas de cerveja
take one down pegue uma
Pass it around. passe adiante
2 bottles of beer on the wall 2 garrafas de cerveja na parede
2 bottles of beer on the wall 2 garrafas de cerveja na parede
2 bottles of beer. 2 garrafas de cerveja
take one down pegue uma
Pass it around. passe adiante
1 bottles of beer on the wall 1 garrafas de cerveja na parede
1 bottle of beer on the wall 1 garrafa de cerveja na parede
…
notem que no final esta com 1 garrafa e ele coloca no plural na primeira vez depois coloca no singular.
corrigi o codigo e ficou assim
[code]
package beersong;
public class Main {
public static void main(String[] args) {
int beerNum=99;
String word = "bottles";
while (beerNum > 0) {
if (beerNum ==1){
word="bottle";
}
if (beerNum > 0){
System.out.println(beerNum+ " " + word + " of beer on the wall " );
}
System.out.println(beerNum+ " " + word + " of beer on the wall " );
System.out.println(beerNum+ " " + word + " of beer." );
System.out.println("take one down " );
System.out.println( "Pass it around. " );
beerNum = beerNum -1;
if (beerNum ==0) {
System.out.println( "no more bottles of ber on the wall" );
}
}
}
}[/code]
Saida com o codigo apresentado:
3 bottles of beer on the wall
3 bottles of beer on the wall
3 bottles of beer.
take one down
Pass it around.
2 bottles of beer on the wall
2 bottles of beer on the wall
2 bottles of beer.
take one down
Pass it around.
1 bottle of beer on the wall
1 bottle of beer on the wall
1 bottle of beer.
take one down
Pass it around.
no more bottles of ber on the wall
desculpe-me ele ter ficado simplificado mas não quis fugir do que foi ensinado no livro, então usei apenas o que foi passado no livro.
O que eu fiz foi apenas repetir o código na mesma forma que coloquei na primeira condição ( beernumb ==0). Ficou da seguinte forma:
public static void main (String[] args){
int beernumb = 99;
String word = “bootles”;
while (beernumb > 0 ) {
if (beernumb==1) {
word = "bootle";
System.out.println(beernumb +" "+word +" of beer on the wall");
System.out.println(beernumb +" "+word + " of beer");
System.out.println("Take out down");
System.out.println("Pass it around");
beernumb = beernumb-1;
}
if (beernumb > 0) {
System.out.println(beernumb+" "+word + " of beers on the wall");
System.out.println(beernumb +" "+word + " of beer");
System.out.println("Take out down");
System.out.println("Pass it around");
beernumb = beernumb-1;
}
else
{System.out.println("No more bootles of beer on the wall"); }
}
}
SAÍDA:
99 bootles of beers on the wall
99 bootles of beer
Take out down
Pass it around
98 bootles of beers on the wall
98 bootles of beer
…
1 bootle of beer on the wall
1 bootle of beer
Take out down
Pass it around
No more bootles of beer on the wall
O problema pelo que verifiquei não é a ordem e sim fazer com que a saia no output a concatenação correta para UMA garrafa ou seja, no singular!