"99 garrafas de cerveja" (duvida) [RESOLVIDO]

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");
		}
	}
}

}
[/code]

Desde já, Agradeço!

É…
Ahn?
Qual a dúvida?

http://99-bottles-of-beer.net/language-java-3.html :smiley:

cadê a duvida??

vc ta rodando pelo prompt de comando do windows né?

ele tem um limite de caracteres, por isso ele limpa o inicio…
rode pelo eclipse mesmo…

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.

Obrigado!

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" : "")); }

nephestos Isso seria abordado em outra ocasião. Obrigado!

Meu foco está na possível falha.

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… :slight_smile:

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"); }

Há uma situação em que vai aparecer “1 bottles”. Será isso?

1 curtida

Basta mover o if para depois da subtração.

1 curtida

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!

Obrigado!

Não. As garrafas estão (cheias?) em cima de uma parede (ou de um muro) e ele vai tirando uma a uma até não ficar nenhuma no muro.

1 curtida

pqp 99 garrafas de cerveja… acho que este sujeito vai acabar passando muito mal…

Concerteza! :smiley:

RESOLVIDO!
Obrigado pelo apoio pessoal.

Ficou por entendido o seguinte:
Como eu não sabia e estava óbvio (BeerSong) isso é uma musica (99 bottles of beer on the wall).

A musica em si ajuda a entender o que o código quer.

Então [color=darkred]a falha está na ordem em que as frases aparecem e na contagem.[/color]

saída correta:

“99 bottles of beer on the wall, 99 bottles of beer.”
"Take one down and pass it around, "
“98 bottles of beer on the wall.”

E assim por diante.

Agora vou arrumar meu código que já estou de SACO CHEIO de ESCUTAR ESSA MUSIQUINHA. :smiley:

Esse vídeo:
Parte 1: [youtube]http://www.youtube.com/watch?v=vWKKuBblumo&feature=related[/youtube]
Parte 2: [youtube]http://www.youtube.com/watch?v=n_C7Rj2qMMs&feature=related[/youtube]
Parte 3: [youtube]http://www.youtube.com/watch?v=PyEMGDa8Jcg&feature=related[/youtube]

A apresentão do video é vã, vejam a letra original da música e a solução apresentada por mim neste link http://www.guj.com.br/java/59842-99-garrafas-de-cerveja/2

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.

1 curtida

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!

Se estiver incorreto me corrijam

Abraços!!