eevandro 0 Denunciar post Postado Abril 24, 2013 vou postar duas perguntas aqui e ver se o programa serve para os dois ou só pra um... 1-Faça um programa que leia duas matrizes 4x4 de números reais, calcule e mostre a matriz resultante da multiplicação entre elas 2-Escreva um programa Java que leia um vetor de 50 posições de números inteiros e mostre somente os positivos. import java.util.Scanner; public class { public static void main(String[] args) { Scanner ler = new Scanner(System.in); int i, j, m[][] = new int[0][50]; for (i=0; i<2; i++) { System.out.printf("Informe os elementos %da. linha:\n", (i+1)); for (j=0; j<4; j++) { System.out.printf("m[%d][%d] = ", i, j); m[i][j] = ler.nextInt(); } System.out.printf("\n"); } System.out.printf("\n"); for (i=0; i<2; i++) { System.out.printf("%da. linha: ", (i+1)); for (j=0; j<4; j++) { System.out.printf("%d ", m[i][j]); } System.out.printf("\n"); } } } Compartilhar este post Link para o post Compartilhar em outros sites
reebr 94 Denunciar post Postado Maio 4, 2013 No primeiro, você está trabalhando errado com os indices, a matriz é 4 x 4 e não 2 x 4, dê uma olhada novamente no seu loop for. Eu recomendo você criar a matriz como um atributo da classe. O segundo, seria isso aqui: public class Main { public static void main(String args[]){ new Numeros(); } } import java.util.Scanner; public class Numeros { public Numeros(){ preencherVetor(); mostrarNumPositivos(); } //muda aqui de "10" para "50" xD final int maxTam = 10; private int []num = new int[maxTam]; private void preencherVetor() { Scanner input = new Scanner(System.in); for(int i = 0; i < maxTam; i++){ System.out.print((i + 1) + " elemento > "); this.num[i] = input.nextInt(); } } private void mostrarNumPositivos() { System.out.println("NUMEROS POSITIVOS: "); for(int i = 0; i < maxTam; i++) if(this.num[i] > 0) System.out.print(this.num[i] + " > "); } } Compartilhar este post Link para o post Compartilhar em outros sites