Wikipedia

Support Wikipedia

2009-08-19

Mas JAVA

Un ejemplo de matrices en java, muy utiles, la idea es poder leer 3 notas de 2 ramos y entregar los promedios correspondientes, en realidad para este ejemplo en particular resulta mas sencillo leer nota a nota y sacar los promedio directamente, el problema aparece cuando se quieren agregar mas datos, de ahi la utilidad de las matrices.
----------------------------------------------------------------------------------------------------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package clase2;
import java.io.*;
/**
*
* @author fran
*/

public class NewClass1 {
/*private int matriz1;
private int [][] data;
*/
public static void main(String[] args) {
InputStreamReader Isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader (Isr);
try{
//int suma=0;
int suma1=0, suma2=0, temp=0;
int prom1=0, prom2=0;
int [][] matriz1 = new int [2][3];
for(int i=0; i<2; i++){
for(int j=0;j<3;j++){
System.out.println("ingrese las notas");
matriz1[i][j] =Integer.parseInt(br.readLine());
}
}
for(int i=0;i<2;i++){
suma2=0;
for(int j=0;j<3;j++){
temp =temp +matriz1[i][j];
suma2=suma2 +matriz1[i][j];
suma1=temp-suma2;
prom1 = suma1/3;
prom2 = suma2/3;
}
}

System.out.println("como ayuda imprimero los numeros segun su ubicacion e la matriz\n");
System.out.println("numeros (matriz1[0][0]): "+ matriz1[0][0]);
System.out.println("numeros (matriz1[0][1]): "+ matriz1[0][1]);
System.out.println("numeros (matriz1[0][2]): "+ matriz1[0][2]);
System.out.println("----------------------------------------");
System.out.println("numeros (matriz1[1][0]): "+ matriz1[1][0]);
System.out.println("numeros (matriz1[1][1]): "+ matriz1[1][1]);
System.out.println("numeros (matriz1[1][2]): "+ matriz1[1][2]);
System.out.println("----------------------------------------");
System.out.println("sumas es: "+ suma1);
System.out.println("sumas es: "+ suma2);
System.out.println("promedio es: "+ prom1);
System.out.println("promedio es: "+ prom2);
}catch (Exception e)
{
e.printStackTrace();
}
}
}