Compare commits

..
18 Commits
15 changed files with 284 additions and 431618 deletions

No files matched your search

+1 -431610
View File
File diff suppressed because it is too large. Load diff
+1
View File
@@ -1,4 +1,5 @@
// Diseña un algoritmo que visualice los 50 primeros números enteros.
//
void main() {
int i = -1;
while (++i < 50)
+19 -6
View File
@@ -4,24 +4,37 @@
import java.util.Scanner;
void main() {
long result = 0;
long a = 0, e = 0, i = 0, o = 0, u = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Cuantos carácteres a leer?");
int n = sc.nextInt();
sc.nextLine();
System.out.println("Introduce los carácteres (uno por línea): ");
System.out.println("Introduce los carácteres: ");
do {
char c = sc.nextLine().charAt(0);
char c = sc.next().charAt(0);
switch (c) {
case 'a', 'e', 'i', 'o', 'u':
result++;
case 'a':
a++;
break;
case 'e':
e++;
break;
case 'i':
i++;
break;
case 'o':
o++;
break;
case 'u':
u++;
break;
default:
}
} while (--n > 0);
sc.close();
System.out.printf("Hay %d vocales\n", result);
System.out.printf("a: %d\ne: %d\ni: %d\no: %d\nu: %d\n", a, e, i, o, u);
}
+5 -2
View File
@@ -11,7 +11,10 @@ void main() {
int n2 = sc.nextInt();
sc.close();
while(n1 <= n2)
System.out.printf("%d, ", n1++);
int mayor = Math.max(n1, n2);
int menor = Math.min(n1, n2);
while(menor <= mayor)
System.out.printf("%d, ", menor++);
System.out.println();
}
+16
View File
@@ -0,0 +1,16 @@
import java.util.Random;
public class Dados {
public static void main(String[] args){
Random r = new Random();
final int TIRADAS = 50;
int total = 0, max = 0, min = 7;
for(int i = 0; i < TIRADAS; i++){
int dado = r.nextInt(6) + 1;
total += dado;
if(dado > max) max = dado;
if(dado < min) min = dado;
}
System.out.printf("El promedio es %.2f, el maximo es %d y el minimo %d\n", (double)total/TIRADAS, max, min);
}
}
+25
View File
@@ -0,0 +1,25 @@
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");
}
}
}
}
+10
View File
@@ -0,0 +1,10 @@
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 = %2d\n", i, j, i*j);
}
System.out.println("--------------");
}
}
}
+20
View File
@@ -0,0 +1,20 @@
import java.util.Scanner;
public class EstructuraRepetitiva3_1 {
static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Introduce un número entero: ");
long n = sc.nextLong();
boolean primo = true;
for (int i = 2; i <= Math.sqrt(n) && primo; i++) {
if (n % i == 0) {
primo = false;
}
}
if(primo)
System.out.println("El número es primo");
else
System.out.println("El número es compuesto");
sc.close();
}
}
+35
View File
@@ -0,0 +1,35 @@
// 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 = 0;
try{
n = sc.nextInt();
} catch (Exception e) {
System.out.println("La lectura de input ha lanzado la exception" + e + "\n");
}
if (n <= 4){
System.out.println("El numero tiene que ser mayor que 4");
return;
}
// 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();
}
}
+23
View File
@@ -0,0 +1,23 @@
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+=3) {
boolean primero = true; // Para poner la , bien
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();
}
}
+30
View File
@@ -0,0 +1,30 @@
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");
System.out.printf("Te has pasado por %.2fKg\n", CARGA_MAXIMA - carga);
} else {
System.out.println("El mueble ya no cabe en el camión");
System.out.printf("Te has pasado por %.2fm3\n", VOLUMEN_MAXIMO - volumen);
}
}
}
+18
View File
@@ -0,0 +1,18 @@
void main() {
Scanner sc = new Scanner(System.in);
sc.useLocale(Locale.US);
double num1 = 0.0;
boolean interruptor = true;
while(interruptor) {
try {
System.out.println("Introduce un real (con .)");
num1 = sc.nextDouble();
System.out.printf("Has introducido: %f\n", num1);
interruptor = false;
} catch (InputMismatchException e) {
System.out.println("No has introducido un real");
} finally {
sc.nextLine();
}
}
}
+23
View File
@@ -0,0 +1,23 @@
import java.util.Random;
public class GeneradorContraseñas {
public static void main(String[] args){
Random r = new Random();
int len = r.nextInt(12 + 1 - 8) + 8; // 8-12
for(int i = 0; i < len; i++) {
switch (r.nextInt(3)) {
case 0: // Dígito
System.out.printf("%d", r.nextInt(10));
break;
case 1: // Minúscula
System.out.printf("%c", r.nextInt(26) + (int) ('a'));
break;
case 2: // Mayúscula
System.out.printf("%c", r.nextInt(26) + (int) ('A'));
break;
default:
}
}
System.out.println();
}
}
+33
View File
@@ -0,0 +1,33 @@
// Tenemos una clase de 40 alumnos que tienen 7 asignaturas cada uno de ellos.
// Haz un porgrama que pregunte la nota de cada asignatura para cada alumno y nos
// diga la nota media de cada asignatura, así como el porcentaje de aprobados
import java.util.*;
public class NotaMedia {
static void main(String[] args) {
Scanner sc = new Scanner(System.in);
final int ALUMNOS = 40;
final int ASIGNATURAS = 7;
for (int i = 0; i < ASIGNATURAS; i++) {
int suspensos = 0;
double notaMedia = 0;
for (int j = 0; j < ALUMNOS;) {
System.out.printf("Qué ha sacado el alumno %2d en la asignatura %d?\n", j, i);
double nota = 0;
try {
nota = sc.nextDouble();
} catch (InputMismatchException e){
System.out.println("ERROR: Pon un número decimal válido");
continue;
} catch (Exception e){
System.out.println("ERROR: el programa ha salido con una exception" + e);
}
if (nota < 5) suspensos++;
notaMedia += nota;
j++;
}
System.out.printf("La nota media de la clase en la asignatura %d es %.2f\n", i, notaMedia / ALUMNOS);
}
}
}
+25
View File
@@ -0,0 +1,25 @@
// Diseña un alrogitmo que cree un número aleatorio y te permita
// itroducir otros números hasta que lo adivines.
void main() {
Scanner sc = new Scanner(System.in);
Random r = new Random();
int secreto = r.nextInt(100) + 1; // 1-100
boolean interruptor = true;
int n;
do {
System.out.println("Adivina el número (1-100)");
n = sc.nextInt();
if (n > secreto) {
System.out.println("Te has pasado");
} else if (n < secreto) {
System.out.println("Te has quedado corto");
} else {
System.out.println("Has acertado!!!");
interruptor = false;
}
} while (interruptor);
sc.close();
}