Exercício Use a Cabeça! Java

10 respostas
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?.

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

}
}
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

10 Respostas

jks1903
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?.

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

}
}
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

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?

D

jks1903,

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

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"); 
}
jks1903
DiiH:
jks1903, Seria isso :
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");
	}
}
}
}

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.

javahunter
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?

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

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

}
}
D

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

jks1903

viajei, no post, desconsiderem, rsrs.

javahunter

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

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

jks1903

javahunter:
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

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

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.

javahunter

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.

era isso jks1903, agora foi… vlw

Criado 10 de outubro de 2012
Ultima resposta 11 de out. de 2012
Respostas 10
Participantes 3