Skip to content

Instantly share code, notes, and snippets.

@earissola
Last active August 29, 2015 14:07
Show Gist options
  • Save earissola/dee4094af8baa8aaaaf1 to your computer and use it in GitHub Desktop.
Save earissola/dee4094af8baa8aaaaf1 to your computer and use it in GitHub Desktop.
[POO] Iteraciones - Demo (Java)
import java.util.Random;
import java.util.Scanner;
public class Iteraciones {
public void forEjemplo1() {
}
public static void main (String[] args){
System.out.println(" ** ## Demos de Ciclos: for ## **");
/* ** Ejemplo 1 **: Ilustra los bucles "for"
* enumerando todas las letras minúsculas ASCII. **/
System.out.println(); // Línea en blanco
System.out.println(" * Ejemplo 1: ");
for(char c = 0; c < 128; c++)
if (Character.isLowerCase(c))
System.out.println("Valor ASCII: " + (int) c +
" caracter: " + c);
/* ** Ejemplo 2 **: el operador "," (coma) sólo tiene un uso en Java: en la
* expresión de control de un bucle "for" para enumerar una serie de instrucciones,
* donde tales instrucciones serán evaluadas secuencialmente **/
System.out.println(); // Línea en blanco
System.out.println(" * Ejemplo 2: ");
for (int i = 1, j = i + 10; i < 5; i++, j = i * 2)
System.out.println(" i = " + i + " j = " + j);
/* ** Ejemplo 3 **: Do-While **/
System.out.println(); // Línea en blanco
System.out.println(" * Ejemplo 3: ");
Scanner miScanner = new Scanner(System.in);
int valor;
do {
System.out.print("Ingrese un valor entre 0 y 999 (0 para Finalizar):");
valor = miScanner.nextInt();
if (valor >= 100) {
System.out.println("Tiene 3 dígitos");
} else {
if (valor>=10) {
System.out.println("Tiene 2 dígitos");
} else {
System.out.println("Tiene 1 dígito");
}
}
} while (valor!=0);
/* ** Ejemplo 4 **: While **/
System.out.println(); // Línea en blanco
System.out.println(" * Ejemplo 4: ");
int x;
x = 1;
Random rand = new Random();
while (x <= 10) {
System.out.println("x = " + x);
x += rand.nextInt(9) + 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment