Matriz com fatorial

2 respostas
V

Boa Tarde gostaria se alguem pudesse me ajudar nesse meu código ele na vdd não me retorna os valores fatoriais que gostaria e não entendo o pq alguem poderia me ajudar...

int num[] [] = new int [3][5];
	int i, j = 0;
	
	for (i = 0;  i < 3; i++){
	    for (j = 0; j < 5; j++){
	          
	          num[i][j]= Integer.parseInt(JOptionPane.showInputDialog("Informe um numero " + j + " matriz A"));
	    }
	  }
	for (i = 0; i < 3; i ++){
	       for (j = 0;j  < 5;j++){
	    	   	    	   
	    	   if(i==0){
	    		   num[i][j] = num[i][j]+5; 
	    	   }
	    	   else if (i==1){
	    		   num[i][j]= fatorial (j);
	    		  		  		  
	    	   }
	    	   else {
	    		   num[i][j] = num[i][j]*num[i][j];
	    	   }
	    	   
	    	    System.out.print(num[i][j]+ "  "); 		   	   
	         	 
	       }
	       System.out.println("  ");
}	
}

	static int fatorial (int n){   
        if (n == 1 || n==0) return 1;   
  
        return ((fatorial (n-1)) * n);   
    }   
}

2 Respostas

G

O que te levou a crer que vc não está tendo os resultados corretos?
Olha o seguinte teste com teu próprio método:

public class Fatorial{

	public int fatorial(int n){   
        if (n == 1 || n==0) return 1;   
        return ((fatorial (n-1)) * n);   
    }   
	
	public static void main(String[] args) {
		int fat = new Fatorial().fatorial(5);
		System.out.println(fat);
	}
}

Retornado valor correto!
Ou seja, tem certeza que o erro é realmente com o fatorial?
Já checou o valor que está sendo passado?

flw.

V
gujuser:
O que te levou a crer que vc não está tendo os resultados corretos? Olha o seguinte teste com teu próprio método:
public class Fatorial{

	public int fatorial(int n){   
        if (n == 1 || n==0) return 1;   
        return ((fatorial (n-1)) * n);   
    }   
	
	public static void main(String[] args) {
		int fat = new Fatorial().fatorial(5);
		System.out.println(fat);
	}
}
Retornado valor correto! Ou seja, tem certeza que o erro é realmente com o fatorial? Já checou o valor que está sendo passado?

flw.

ctza ja verifiquei e deu certo meu erro foi bem besta...

else if (i==1){   
                   num[i][j]= fatorial (j);
               }   
 // eu deixei assim enquanto na verdade deveria ter escrito assim num[i][j] = fatorial (num1[j]);  isso dai transformei num vetor la em cima ao invês de uma matriz... meu problema está OK
Criado 26 de março de 2009
Ultima resposta 26 de mar. de 2009
Respostas 2
Participantes 2