0% encontró este documento útil (0 votos)
290 vistas7 páginas

Ejercicios Java For Do While

Los 19 ejercicios presentan ejemplos del uso de diferentes estructuras de control de flujo en Java como for, while, do-while. Cada ejercicio contiene un pequeño programa que ilustra el uso de una estructura para realizar tareas como iterar sobre arrays, sumar números ingresados, o imprimir valores de variables.

Cargado por

ZeHenry
Derechos de autor
© © All Rights Reserved
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como TXT, PDF, TXT o lee en línea desde Scribd
0% encontró este documento útil (0 votos)
290 vistas7 páginas

Ejercicios Java For Do While

Los 19 ejercicios presentan ejemplos del uso de diferentes estructuras de control de flujo en Java como for, while, do-while. Cada ejercicio contiene un pequeño programa que ilustra el uso de una estructura para realizar tareas como iterar sobre arrays, sumar números ingresados, o imprimir valores de variables.

Cargado por

ZeHenry
Derechos de autor
© © All Rights Reserved
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como TXT, PDF, TXT o lee en línea desde Scribd
Está en la página 1/ 7

ejercicios

Ejercicio 1 (for)
package tzejercicio01;

public class Tzejercicio01 {

public static void main(String[] args) {


// TODO code application logic here
for (int tzcontador = 0; tzcontador<=15 ; tzcontador ++)
{
System.out.println(tzcontador);
if(tzcontador==10) break;
if(tzcontador==10) continue;
System.out.println("Despues del if");
}
}
}

Ejercicio 2 (while)
Package tzejercicio02;
import java.util.Scanner;
public class Tzejercicio02 {

public static void main(String[] args) {


// TODO code application logic here
Scanner tzentrada=new Scanner(System.in);
int tzdecimal;
System.out.println("Ingresar un numero deciaml mayor a 1000: ");
tzdecimal=tzentrada.nextInt();
String binario = "";
while ( tzdecimal > 0 ) {
binario = tzdecimal % 2 + binario;
tzdecimal /= 2;
}
System.out.println(tzdecimal+" en numeros binarios es: "+binario);
}
}
Ejercicio 3 (while)
package tzejercicio03;
import java.util.Scanner;
public class Tzejercicio03 {
public static void main(String[] args) {
// TODO code application logic here
System.out.println("Programa para sumar varios números");
Scanner tzentrada = new Scanner(System.in);
int tzacumulador = 0;
int tznuevoNumero = 0;
while (tznuevoNumero >= 0) {
System.out.println("Insertar un nuevo número a sumar o -1 para finalizar");
tznuevoNumero = tzentrada.nextInt();
if (tznuevoNumero > 0) {
tzacumulador = tzacumulador + tznuevoNumero;
}
}
System.out.println("Total: " + tzacumulador);
}
}
Ejercicio 4 (do-while)
package tzejercicio04;
import java.util.Scanner;
public class Tzejercicio04 {

public static void main(String[] args) {


// TODO code application logic here
int tznum, tzsuma=0;
Scanner tzentrada = new Scanner(System.in);
do {
System.out.print("Ingrese un número: ");
System.out.println("Ingresar 0 para terminar la accion");
tznum = tzentrada.nextInt();
tzsuma+=tznum;
} while (tznum != 0);
System.out.println("La suma total da: " + tzsuma);
}
}
Ejercicio 5 (for)
package tzejercicio05;
public class Tzejercicio05 {
public static void main(String[] args) {
// TODO code application logic here
System.out.println("Numeros menores a 100");
for (int tzi = 1; tzi < 100; tzi++) {
System.out.println("El valor de i es: " + tzi);
if (tzi== 100) {
break;
}
}
}
}
Ejercicio 6 (for)
package tzejercicio06;

public class Tzejercicio06 {

public static void main(String[] args) {


// TODO code application logic here
System.out.println("Marcas de Carros");
String[] tzcars = {"Volvo", "BMW", "Ford", "Mazda"};
for (String tzi : tzcars) {
System.out.println(tzi);
}
}
}

Ejercicio 7 (while)
package tzejercicio07;
import java.util.Scanner;
public class Tzejercicio07 {
public static void main(String[] args) {
// TODO code application logic here
System.out.println("Programa para sumar varios números");
Scanner tzscanner = new Scanner(System.in);
int tzacumulador = 0;
int tznuevoNumero = 0;
while (tznuevoNumero >= 0) {
System.out.println("Insertar un nuevo número a sumar o -1 para
finalizar");
tznuevoNumero = tzscanner.nextInt();
if (tznuevoNumero > 0) {
tzacumulador = tzacumulador + tznuevoNumero;}}
System.out.println("Total: " + tzacumulador);
}
}
Ejercicio 8 (do-while)
package tzejercicio08;
import java.util.Scanner;
public class Tzejercicio08 {

public static void main(String[] args) {


// TODO code application logic here
int tzEdad, tzSuma = 0,tzContador = 0,tzPromedio=0;
Scanner tzTeclado=new Scanner(System.in);
do {
System.out.println("Ingrese la edad de la persona");
tzEdad=tzTeclado.nextInt();
tzSuma=tzSuma+tzEdad;
tzContador=tzContador+1;
tzPromedio=tzSuma/tzContador;
System.out.println("El promedio por ahora es " +tzPromedio);
if (tzPromedio>25) {
break;}
} while (tzEdad>0);
System.out.println("LA suma de las edades es :" + tzSuma + " La Cantidad de
edades ingresadas fueron: "+ tzContador);
System.out.println("El promedio es igual a: "+tzPromedio);
if (tzPromedio>25) {
System.out.println("El promedio fue mayor que 25 y ha finalizado");
}else {
System.out.println("Reinicia el programa ya que el promedio no fue superior a
25");
}
}
}

Ejercicio 9 (do-while)
package tzejercicio09;
import java.util.Scanner;
public class Tzejercicio09 {
public static void main(String[] args) {
// TODO code application logic here
int tzNota = 0, tzCont1 = 0,tzCont2 = 0;
Scanner tzTeclado = new Scanner(System.in);
do {
System.out.println("Ingrese la nota del alumno menor a 20 y digite 0 para
terminer la accion ");
tzNota = tzTeclado.nextInt();
if (tzNota>=11) {
tzCont1=tzCont1+1;
} else if (tzNota>0){
tzCont2=tzCont2+1;
}
} while (tzNota != 0);
System.out.println("La cantidad de alumnos que tienen notas mayores o iguales a
11 son : " + tzCont1);
System.out.println("La cantidad de alumnos que tienen notas menores a 11 son:
"+tzCont2);
}
}
Ejercicio 10 (do-while)
package tzejercicio10;
import java.util.Scanner;
public class Tzejercicio10 {
public static void main(String[] args) {
// TODO code application logic here
Scanner tzentrada=new Scanner(System.in);
int tzvalor;
do {
System.out.print("Ingrese un valor entre 0 y 50 (0 finaliza):");
tzvalor=tzentrada.nextInt();
if (tzvalor>=50) {
System.out.println("Es de 3 dígitos.");
} else {
if (tzvalor>=10) {
System.out.println("Es de 2 dígitos.");
} else {
System.out.println("Es de 1 dígito.");
}
}
} while (tzvalor!=0);
}
}
Ejercicio 11 (while)
package tzejercicio11;
public class Tzejercicio11 {
public static void main(String[] args) {
// TODO code application logic here
int tzx = 10;
while( tzx <= 50 ) {
System.out.print("todos los numeroes menores o igual que x : " + tzx );
tzx++;
System.out.print("\n");
} } }

Ejercicio 12 (for)
package tzejercicio12;
public class Tzejercicio12 {
public static void main(String[] args) {
// TODO code application logic here
String[] tzdias = {"Lunes", "Martes", "Miercoles", "Jueves", "Viernes",
"Sabado", "Domingo"};

System.out.println("Los dias de la semana son:");


for (String tzdia : tzdias) {
System.out.println(tzdia);
}
}
}
Ejercicio 13 (do-while)
package tzejercicio13;
public class Tzejercicio13 {
public static void main(String[] args) {
// TODO code application logic here
final boolean tzi = false;

do {
System.out.println("¿Hoy es mi cumpleaños?");
} while(tzi);

System.out.println(tzi);
}
}
Ejercicio 14 (while)
package tzejercicio14;
public class Tzejercicio14 {
public static void main(String[] args) {
// TODO code application logic here
int tzi = 0;

while (true) {

tzi++;

System.out.println ("Valor de tzi: " + tzi);

if (tzi==9) { break;}
}
}
}
Ejercicio 15 (for)
package tzejercicio15;

public class Tzejercicio15 {


public static void main(String[] args) {
// TODO code application logic here
for(int tzi = 0, tzf = 6; tzi < 10 && tzf > 0; tzi++, tzf--) {
System.out.print(tzi + " - " + tzf + " ");
}
}
}

Ejercicio 16 (for)
package tzejercicio16;

public class Tzejercicio16 {

public static void main(String[] args) {


// TODO code application logic here
char tzb;
for(tzb = 0x0041; tzb < 0x0045; tzb++) {
System.out.println(tzb);
}
System.out.println("FUERA DEL BUCLE");
}
}
Ejercicio 17 (for-while)
package tzejercicio17;
public class Tzejercicio17 {
public static void main(String[] args) {
// TODO code application logic here
for (int tzi = 5; tzi != 0; tzi -= 2)
{
System.out.println(tzi);
}
int tzx = 5;
// bucle infinito
while (tzx == 5)
{
System.out.println("En el bucle infinito");
}
}
}
Ejercicio 18 (for)
package tzejercicio18;
import java.util.Scanner;
public class Tzejercicio18 {

public static void main(String[] args) {


System.out.println("Ingrese un numero menor o igual a 4");
Scanner tzentrada=new Scanner(System.in);
int tzx;
tzx=tzentrada.nextInt();
for ( tzx = 2; tzx <= 4; tzx++)
System.out.println("Valor de x: " + tzx);
}
}

Ejercicio 19 (do-while)
package tzejercicio19;
import java.util.Scanner;
public class Tzejercicio19 {
public static void main(String[] args) {
// TODO code application logic here
int tzx;
Scanner tzentrada=new Scanner(System.in);
System.out.println("Ingresar un numero menor a 20 y mayor a 0");
tzx=tzentrada.nextInt();
do{
System.out.print("numero menor que tzx : " + tzx );
tzxx++;
System.out.print("\n");
}while( tzxx < 20 );
}
}
Ejercicio 20 (while)
package tzejercicio20;

public class Tzejercicio20 {

public static void main(String[] args) {


// TODO code application logic here
int tzi = 0;

while(tzi != 0) {
System.out.println("HUNAR"); // No se ejecuta.
}

do {
System.out.println("HUNAR-TAIPE"); // Se ejecuta una vez.
} while(tzi != 0);
}
}

También podría gustarte