Ejercicios estructura repetitiva
This commit is contained in:
1 parent
70b99a0a23
commit
99ade1ddb3
6 files changed
+113
-16
No files matched your search
@@ -1,3 +1,25 @@
|
||||
void main() {
|
||||
$END$
|
||||
import java.util.*;
|
||||
|
||||
public class EstructuraRepetitiva1 {
|
||||
public static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
boolean repetir = true;
|
||||
while(repetir){
|
||||
System.out.printf("1.-Opción1\n2.-Opción2\n3.-Opción3\n4.-Finalizar\n");
|
||||
String opt = sc.nextLine();
|
||||
switch(opt){
|
||||
case "1", "2", "3":
|
||||
System.out.println("============================");
|
||||
System.out.printf("Has seleccionado la opción %s\n", opt);
|
||||
System.out.println("============================");
|
||||
break;
|
||||
case "4":
|
||||
System.out.println("Finalizando proceso");
|
||||
repetir = false;
|
||||
break;
|
||||
default:
|
||||
System.out.println("Opción inválida");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,10 @@
|
||||
void main() {
|
||||
$END$
|
||||
public class EstructuraRepetitiva2 {
|
||||
public static void main(String[] args) {
|
||||
for (int i = 1; i <= 9; i++) {
|
||||
for (int j = 1; j <= 9; j++) {
|
||||
System.out.printf("%2d x %2d = %3d\n", i, j, i*j);
|
||||
}
|
||||
System.out.println("--------------");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,12 @@
|
||||
import java.util.*;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class EstructuraRepetitiva3_1 {
|
||||
public static void main(String[] args) {
|
||||
static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.println("Introduce un número entero: ");
|
||||
long n = sc.nextLong();
|
||||
for(int i = 2; i <= Math.sqrt(n); i++){
|
||||
if(n % i == 0){
|
||||
for (int i = 2; i <= Math.sqrt(n); i++) {
|
||||
if (n % i == 0) {
|
||||
System.out.println("El número es compuesto");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,3 +1,24 @@
|
||||
void main() {
|
||||
$END$
|
||||
}
|
||||
// Sieve of Eratosthenes
|
||||
import java.util.Scanner;
|
||||
|
||||
public class EstructuraRepetitiva3_2 {
|
||||
static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.println("Introduce un número entero: ");
|
||||
int n = sc.nextInt();
|
||||
|
||||
// si compuestos[n] == true, el número es compuesto
|
||||
boolean[] compuestos = new boolean[n + 1];
|
||||
for (int i = 2; i <= n; i++) {
|
||||
if (!compuestos[i])
|
||||
for (int j = i * 2; j <= n; j += i)
|
||||
compuestos[j] = true;
|
||||
}
|
||||
System.out.printf("Estos son todos los números primos menores que %d:\n", n);
|
||||
for (int i = 2; i <= n; i++) {
|
||||
if (!compuestos[i]) System.out.printf("%d ", i);
|
||||
}
|
||||
|
||||
sc.close();
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,25 @@
|
||||
void main() {
|
||||
$END$
|
||||
}
|
||||
import java.util.Scanner;
|
||||
|
||||
public class EstructuraRepetitiva4 {
|
||||
static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.println("Introduce un número entero: ");
|
||||
int n = sc.nextInt();
|
||||
|
||||
for (int i = 3; i <= n; i++) {
|
||||
boolean primero = true; // Para poner la , bien
|
||||
if (i % 3 == 0) {
|
||||
System.out.printf("Múltiplo=%d divisores del %d: (", i, i);
|
||||
for (int j = 1; j <= i; j++) {
|
||||
if (i % j == 0) {
|
||||
if (!primero) System.out.print(",");
|
||||
System.out.printf("%d", j);
|
||||
primero = false;
|
||||
}
|
||||
}
|
||||
System.out.print(")\n");
|
||||
}
|
||||
}
|
||||
sc.close();
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,28 @@
|
||||
void main() {
|
||||
$END$
|
||||
import java.util.Scanner;
|
||||
|
||||
public class EstructuraRepetitiva5 {
|
||||
static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
final double CARGA_MAXIMA = 27800.00; // Kg
|
||||
final double VOLUMEN_MAXIMO = 26.75; // M^3
|
||||
double carga = 0.00, volumen = 0.00;
|
||||
while (carga < CARGA_MAXIMA && volumen < VOLUMEN_MAXIMO) {
|
||||
System.out.println("Introduce el peso y volumen (peso volumen): ");
|
||||
try {
|
||||
carga += sc.nextDouble();
|
||||
volumen += sc.nextDouble();
|
||||
} catch (java.util.InputMismatchException e) {
|
||||
System.out.println("Formato de número inválido");
|
||||
continue;
|
||||
} catch (Exception e) {
|
||||
System.out.println("La lectura de input ha lanzado la exception" + e + "\n");
|
||||
}
|
||||
System.out.printf("Peso: %.2f, Volúmen: %.2f\n", carga, volumen);
|
||||
}
|
||||
if (carga >= CARGA_MAXIMA) {
|
||||
System.out.println("El camión ha alcanzado el peso máximo");
|
||||
} else {
|
||||
System.out.println("El mueble ya no cabe en el camión");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user