Esto es lo q tengo hecho
/** * AWT Sample application * * @author * @version 1.00 09/08/08 */ import java.io.*; public class pruebaProyecto7 { public static void main (String[] args) throws IOException { BufferedReader dis; dis = new BufferedReader(new InputStreamReader(System.in)); String Entrada; int opcion = 0; int tam = 0; int mat[][]; mat=new int[tam][tam]; while(true) { muestraMenu(); Entrada = new String(dis.readLine()); opcion = Integer.parseInt(Entrada); //opcion = Entrada; //si se digita 5 sale del ciclo if(opcion == 4) { System.out.println (""); System.out.print ("Saliendo de la aplicacion ..."); break; } if(opcion > 4) { System.out.println (""); System.out.print ("La opcion que ud eligio esta fuera del rango del menu..."); System.out.println (""); //muestraMenu(); } //evalua la opcion seleccionada switch (opcion) { case 1: ingreso(); break; case 2: CreaArchivo(mat, tam); boolean existe; if (existe = false) { ingreso(); System.out.print ("Primero debe llenar la matriz"); } if (existe = true) { System.out.print ("Archivo guardado con el nombre proyecto.txt en C"); CreaArchivo(mat, tam); } } } } public static void muestraMenu() { System.out.println (""); System.out.println ("**********************************"); System.out.println (""); System.out.println ("***************MENU***************"); System.out.println (""); System.out.println (" ** ESCOJA UNA OPCION ** "); System.out.println (""); System.out.println(" 1. LLENAR MATRIZ"); System.out.println(" 2. CREAR ARCHIVO DE TEXTO"); System.out.println(" 3. LEER ARCHIVO DE TEXTO"); System.out.println(" 4. SALIR"); System.out.println (""); System.out.println ("**********************************"); System.out.println (""); } //******************************************** public static void ingreso ()throws IOException { //*******************************
BufferedReader dis; dis = new BufferedReader(new InputStreamReader(System.in)); String ent; //variables string int mat[][]; int i=0,j=0,tam=0,n=1,pos=0,max=0,multiplo=0,cont=1; //variables enteras
System.out.println ("***********************************************************"); System.out.println ("**Digite el tamano de la matriz que sea impar y mayor a 3**"); System.out.println ("***********************************************************"); ent=new String(dis.readLine()); tam=Integer.parseInt(ent); mat=new int[tam][tam]; max=tam*tam; //valor maximo de la matriz o tamaño maximo
if (tam>=3) // evalua si la matriz es impar y mayor a 3 para continuar { pos=tam/2; // identifica la posicion inicial y central del vector j=pos; }
while (n<=max) // hace el proceso mientras el numero insertado en la matriz sea menor o igual al tamaño de la matriz { mat[j]=n; n++; i=i-1; j=j+1; if (i<0) { i=tam-1; } if (j>=tam) { j=0; }
multiplo=tam*cont; //aqui se sacan los multiplos del tamaño de la matriz if (multiplo<max && n==multiplo) //se evaluan esos multiplos { mat[j]=n; i++; n++; mat[j]=n; cont++; //responsable de crear los multiplos de la matriz } } impresion(mat,tam,max,n); //imprime matriz visualización previa
} /****************************************************************************************************************/ public static void CreaArchivo(int mat[][], int tam) { try { //ubicacion y nombre del archivo String nombre = "C:/Proyecto1.txt"; FileWriter fw = new FileWriter(nombre); BufferedWriter bw = new BufferedWriter(fw); PrintWriter salida = new PrintWriter(bw); bw.newLine(); int i = 0; int j = 0; salida.println("ENCABEZADO DEL ARCHIVO"); salida.println("ENCABEZADO1"); salida.println(" "); while (i < tam) { while (j < tam) { salida.print("* "+mat[j]); j++; } salida.println(" "); j=0; i++; } salida.close(); } catch(java.io.IOException ioex) { System.out.println("se presento el error: "+ioex.toString()); } }
/********************************************************************************************/
public static void impresion(int mat[][],int tam,int max,int n)throws IOException { int i,j; System.out.println ("----------Impresion de la matriz-----------------"); for (i=0; i<tam;i++) { for (j=0; j<tam; j++) { if (mat[j]<=max) System.out.print (" "+mat[j]); else if (mat[j]>max) System.out.print (" "+mat[j]); } System.out.println (); } }
/****************************************************************************************************************/ }
|