Exercício Use a Cabeça! Java

Olá a todos,

preciso de ajuda nesse exercício do Use a Cabeça! Java

Colocaremos todas as suas novas aplicações em Java em uso como algo prático. Precisamos de uma classe com método main(), um tipo int e uma variável String, um loop while e teste IF. Mais alguns retoques e você estará construindo esse back-end empresarial sem demora. Mas antes examinará o código dessa rotina e pensará por um instante em você codificaria esse grande clássico, ?99 garrafas de cervejas?.

[code]public class BeerSong {

/**

  • @param args the command line arguments
    */
    public static void main(String[] args) {
    // TODO code application logic here

int beerNum = 99;

String word = “bottles”;

while (beerNum > 0) {

if (beerNum == 1){
word = “bottle”; // no singular, uma Garrafa
}

System.out.println(beerNum + " " + word + " of beer on the wall") ;
System.out.println(“Take one down.”);
System.out.println(“Pass it around”);

if (beerNum > 0){
System.out.println(beerNum + " " + word + " of beer on the wall");

}else{
System.out.println(“No more bottles of beer on the wall”);
}

}
}[/code]
OBS: Ainda há uma pequena falha em nosso código. Ele será compilado e executado, mas a saída não está 100% perfeita. Veja se consegue identificar a falha e corrija.

O código funciona tranquilo, so que tem um problema era pra marca de 99 bottles of beer on the wall até 1 bottles of beer on the wall, mas o compilador marca de 59 bottles of beer on the wall até 1 bottles of beer on the wall

Alguem tem idéia do que pode ser ou isso é normal do prompt de comando

[quote=javahunter]Olá a todos,

preciso de ajuda nesse exercício do Use a Cabeça! Java

Colocaremos todas as suas novas aplicações em Java em uso como algo prático. Precisamos de uma classe com método main(), um tipo int e uma variável String, um loop while e teste IF. Mais alguns retoques e você estará construindo esse back-end empresarial sem demora. Mas antes examinará o código dessa rotina e pensará por um instante em você codificaria esse grande clássico, ?99 garrafas de cervejas?.

[code]public class BeerSong {

/**

  • @param args the command line arguments
    */
    public static void main(String[] args) {
    // TODO code application logic here

int beerNum = 99;

String word = “bottles”;

while (beerNum > 0) {

if (beerNum == 1){
word = “bottle”; // no singular, uma Garrafa
}

System.out.println(beerNum + " " + word + " of beer on the wall") ;
System.out.println(“Take one down.”);
System.out.println(“Pass it around”);

if (beerNum > 0){
System.out.println(beerNum + " " + word + " of beer on the wall");

}else{
System.out.println(“No more bottles of beer on the wall”);
}

}
}[/code]
OBS: Ainda há uma pequena falha em nosso código. Ele será compilado e executado, mas a saída não está 100% perfeita. Veja se consegue identificar a falha e corrija.

O código funciona tranquilo, so que tem um problema era pra marca de 99 bottles of beer on the wall até 1 bottles of beer on the wall, mas o compilador marca de 59 bottles of beer on the wall até 1 bottles of beer on the wall

Alguem tem idéia do que pode ser ou isso é normal do prompt de comando[/quote]

Não cheguei a testar, até pq não vou conseguir no momento, mas esse código não está gerando um loop infinito?

Onde é decrementada a variável beerNum?

jks1903,

Seria isso :

[code]
public class BeerSong {

/**

  • @param args the command line arguments
    */
    public static void main(String[] args) {
    // TODO code application logic here

int beerNum = 99;

String word = "bottles";

while (beerNum > 0) {

if (beerNum == 1){
	word = "bottle"; // no singular, uma Garrafa
}

System.out.println(beerNum + " " + word + " of beer on the wall") ;
System.out.println("Take one down.");
System.out.println("Pass it around");

--beerNum;

if (beerNum > 0){
	System.out.println(beerNum + " " + word + " of beer on the wall");
}else{
	System.out.println("No more bottles of beer on the wall");
}

}
}
}[/code]

Um outro detalhe é que esse trecho de código nunca será alcançado, pois está dentro do laço.

}else{  
       System.out.println("No more bottles of beer on the wall"); 
} 

[quote=DiiH]jks1903,

Seria isso :

[code]
public class BeerSong {

/**

  • @param args the command line arguments
    */
    public static void main(String[] args) {
    // TODO code application logic here

int beerNum = 99;

String word = "bottles";

while (beerNum > 0) {

if (beerNum == 1){
	word = "bottle"; // no singular, uma Garrafa
}

System.out.println(beerNum + " " + word + " of beer on the wall") ;
System.out.println("Take one down.");
System.out.println("Pass it around");

--beerNum;

if (beerNum > 0){
	System.out.println(beerNum + " " + word + " of beer on the wall");
}else{
	System.out.println("No more bottles of beer on the wall");
}

}
}
}[/code][/quote]

Sim, quase isso.

Apenas verifique aquele teste ao fim do laço, que sinceramente, não há razões para existir, visto que o teste já é feito na condição do loop.

[quote]Não cheguei a testar, até pq não vou conseguir no momento, mas esse código não está gerando um loop infinito?

Onde é decrementada a variável beerNum?[/quote]

Vc ta certo jks1903 esqueci de colocar a decrementação.

[code]public class BeerSong {

/**

  • @param args the command line arguments
    */
    public static void main(String[] args) {
    // TODO code application logic here

int beerNum = 99;

String word = “bottles”;

while (beerNum > 0) {

if (beerNum == 1){
word = “bottle”; // no singular, uma Garrafa
}

System.out.println(beerNum + " " + word + " of beer on the wall") ;
System.out.println(“Take one down.”);
System.out.println(“Pass it around”);
beerNum–;

if (beerNum > 0){
System.out.println(beerNum + " " + word + " of beer on the wall");

}else{
System.out.println(“No more bottles of beer on the wall”);
}

}
}[/code]

javahunter,

Rode o codigo que eu postei anteriormente. O problema foi decrementar a variavel beerNum até acabar a beer.

Chegando no valor 0 será apresentado a informação : No more bottles of beer on the wall

viajei, no post, desconsiderem, rsrs.

[quote=DiiH]javahunter,

Rode o codigo que eu postei anteriormente. O problema foi decrementar a variavel beerNum até acabar a beer.

Chegando no valor 0 será apresentado a informação : No more bottles of beer on the wall[/quote]

DiiH sim funciona, esqueci de colocar a decrementação antes =) mas o problema é que era pra marca de 99 bottles of beer on the wall até 1 bottles of beer on the wall na tela do prompt, mas o compilador marca de 59 bottles of beer on the wall até 1 bottles of beer on the wall… queria saber porque ele faz isso

abraço

[quote=javahunter][quote=DiiH]javahunter,

Rode o codigo que eu postei anteriormente. O problema foi decrementar a variavel beerNum até acabar a beer.

Chegando no valor 0 será apresentado a informação : No more bottles of beer on the wall[/quote]

DiiH sim funciona, esqueci de colocar a decrementação antes =) mas o problema é que era pra marca de 99 bottles of beer on the wall até 1 bottles of beer on the wall na tela do prompt, mas o compilador marca de 59 bottles of beer on the wall até 1 bottles of beer on the wall… queria saber porque ele faz isso

abraço

[/quote]

Cara, testei aqui e funcionou, do 99 até o 1.

Apenas imprime duas vezes devido ao teste desnecessário que há no fim do loop.

[quote]Cara, testei aqui e funcionou, do 99 até o 1.

Apenas imprime duas vezes devido ao teste desnecessário que há no fim do loop.
[/quote]

era isso jks1903, agora foi… vlw