// Lab03.java (C) 2007 adolfo@di-mare.com /** @(#)Lab03.java 2007 Ejemplos de uso de \c while() y de \c for(;;). @author Adolfo Di Mare */ import java.io.*; // Clases para leer desde el teclado import java.lang.System; // Esta siempre la importa Java /** Cointiene los métodos que realizan cada uno de los pasos del proceso. */ class Bib { /** CIN es el mecanismo que permite leer hileras del teclado. */ static BufferedReader CIN; // Flujo de entrada estándar /** Muestra cómo se calcula {@code X^n} usando \c {@code while()}. */ static int main_while() throws IOException { long n, i; float X, res; System.out.println(); System.out.println( "Entre X: " ); X = Float.parseFloat( CIN.readLine() ); System.out.println( "Entre n: " ); n = Integer.parseInt( CIN.readLine() ); i = 1; res = 1; while (i <= n) { // while res *= X; ++i; } System.out.println( "X^n == " + (res) ); return 0; } /** Muestra cómo se calcula {@code X^n} usando {@code for(;;)}. */ static int main_for() throws IOException { int n, i; float X, res; System.out.println(); System.out.println( "Entre X: " ); X = Float.parseFloat( CIN.readLine() ); System.out.println( "Entre n: " ); n = Integer.parseInt( CIN.readLine() ); for (i=1, res=1; i <= n; ++i) { // for (;;) res *= X; } System.out.println( "X^n == " + (res) ); return 0; } /** Calcula promedios de clase, usando un valor como sentinela. */ static int main_promedio() throws IOException { int total, // total de notas n, // cantidad de notas ingresadas nota; // una nota float promedio; // promedio, con punto decimal // fase de inicialización n = 0; total = 0; System.out.println(); // fase de proceso System.out.println( "Ingrese cada nota, -1 para terminar: " ); nota = Integer.parseInt( CIN.readLine() ); while ( nota != -1 ) { total = total + nota; n = n + 1; System.out.println( "Ingrese cada nota, -1 para terminar: " ); nota = Integer.parseInt( CIN.readLine() ); } // fase de conclusión if ( n != 0 ) { promedio = (float)(total) / (float)n; System.out.println( "El promedio de la clase es " +(promedio) ); } else { System.out.println( "Ninguna nota fue ingresada" ); } return 0; // indica que el programa terminó con éxito } /** Intercambia V[i] <==> V[j]. */ static void intercambie( char[] V, int i, int j ) { char tmp = V[i]; V[i] = V[j]; V[j] = tmp; } /** Invierte las letras de "{@code L[]}" en el rango {@code [izq..der]} (extremos incluidos).
  • Si (derder) no hace nada. */ static void invierteRango( char[] L, int izq, int der ) { int i,j; for (i=izq, j=der; i Invoca a las demás rutínas {@code main()}. */ public static void main( String args[] ) throws IOException { Bib.CIN = new BufferedReader( new InputStreamReader(System.in) ); int ret; // código de retorno del programa principal ret = Bib.main_invierteRango(); if (ret != 0) { System.out.print( "ERROR: " + (ret) + "main_invierteRango()" ); } ret = Bib.main_promedio(); if (ret != 0) { System.out.print( "ERROR: " + (ret) + "main_promedio()" ); } ret = Bib.main_while(); if (ret != 0) { System.out.print( "ERROR: " + (ret) + "main_while()" ); } ret = Bib.main_for(); if (ret != 0) { System.out.print( "ERROR: " + (ret) + "main_for()" ); } return; } /** Constructor por defecto. * {@code private} previene que la clase sea instanciada. */ private Lab03() { super(); } } // Lab03 // EOF: Lab03.java