Dúvida Código, me expliquem por favor

8 respostas
J

Boa noite,

Por favor, peço que alguém comente este código para que eu entenda. O que faz cada linha?? Não entendi…

class Hobbits{

String nome;

public static void main(String []args){

Hobbits [] h = new Hobbits[3];

int z =-1;

while (z < 2){

z = z + 1;

h[z] = new Hobbits();

h[z].nome = bilbo;

if (z ==1){

h[z].nome = frodo;

}

if (z ==2){

h[z].nome = sam;

}

System.out.print(h[z].nome + " eh ");

System.out.println(Um bom nome para um Hobbit!);

}	

}

}

8 Respostas

LPJava

vc poderia usar as tags do forum? vai ajudar bastante a leitura do code.

link

JxtaNode

Bom dia,

class Hobbits {
	
	String nome; // init var nome with value null 
	
	
	public static void main(String []args)
	{
		// The size of this array is 3 and the arrays in Java begin at index 0
		Hobbits[] h = new Hobbits[3];
		
		int z =-1; // init var z at -1 
		
		// this loop continue while value of z is less than 2
		while (z < 2) 
		{
			z = z + 1; // the firt time z=-1+1 , Attention d'ont forget that when z=1  so z++ = 2
			
			h[z] = new Hobbits();
			h[z].nome = "bilbo";  // the variable nome is put to value "bilbo" when z=0
			
			if (z ==1){
				h[z].nome = "frodo"; // the variable nome is put to value "frodo" when z=1
			}
			if (z ==2){
				h[z].nome = "sam"; // the variable nome is put to value "sam" when z=2
			}
			
			System.out.print(h[z].nome + " eh ");
			System.out.println("Um bom nome para um Hobbit!");
		}
	}
} 

/* OUTPUT  Result : 
 
bilbo eh Um bom nome para um Hobbit!
frodo eh Um bom nome para um Hobbit!
sam eh Um bom nome para um Hobbit!
*/
robson_oliveira

Bom dia…

[REMOVIDO LINK PARA CONTEÚDO PAGO]

davidbuzatto

O Use a Cabeça é gratuido? :shock:

J

Obrigado, me ajudou bastante…

J

Vou seguir as dicas… vlw galera

J

Boa noite,

Por favor, alguém pode me dizer qual é o erro desse código, pois tenho quase certeza que está correto, mas não roda?!!

class Exercicio{ public static void main(String[] args){ for (long fatorial=1, n=1; n<30; n++){ fatorial *=n; System.out.println(fatorial); } } }

davidbuzatto

JoedsonSCosta:
Boa noite,

Por favor, alguém pode me dizer qual é o erro desse código, pois tenho quase certeza que está correto, mas não roda?!!

class Exercicio{ public static void main(String[] args){ for (long fatorial=1, n=1; n<30; n++){ fatorial *=n; System.out.println(fatorial); } } }

Evite esse tipo de construção do for.
Teste assim:

int n = 10; long fatorial = 1; for ( int i = 1; i <= n; i++ ) { fatorial *= i; } System.out.println(fatorial);

Criado 21 de novembro de 2009
Ultima resposta 22 de nov. de 2009
Respostas 8
Participantes 5