Compare commits
33
Commits
85b9532a78
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bfbbfec86d | ||
|
|
96e2bed803 | ||
|
|
d10caff96d | ||
|
|
5e74e07543 | ||
|
|
2738c0c9b5 | ||
|
|
42030706b4 | ||
|
|
8b3b337b03 | ||
|
|
5129dd9415 | ||
|
|
8ee32e659d | ||
|
|
296ce2f729 | ||
|
|
99ade1ddb3 | ||
|
|
70b99a0a23 | ||
|
|
d92b72dcf0 | ||
|
|
be3915fa3c | ||
|
|
64f39d73e1 | ||
|
|
8c5d233def | ||
|
|
537b8aefc0 | ||
|
|
844ec651a1 | ||
|
|
2faefaef88 | ||
|
|
42ccb65310 | ||
|
|
2e8c5cae24 | ||
|
|
cf7bb1f1b9 | ||
|
|
fc32b4682d | ||
|
|
672e3cf27d | ||
|
|
7af7a01592 | ||
|
|
d459661d0a | ||
|
|
54ba317e13 | ||
|
|
12e68911b2 | ||
|
|
c4e0d4f215 | ||
|
|
38fce30e8b | ||
|
|
395ce74a0d | ||
|
|
bd3b1be755 | ||
|
|
3d60270af6 |
No files matched your search
@@ -0,0 +1,58 @@
|
||||
// El jugador comienza una aventura con 100 puntos de vida.
|
||||
//Debe elegir entre tres caminos:
|
||||
// 1. Entrar en el bosque
|
||||
// 2. Entrar en la cueva
|
||||
// 3. Ir al castillo
|
||||
//Según la elección:
|
||||
//Bosque
|
||||
// Si tiene 70 puntos de vida o más → encuentra un tesoro.
|
||||
// Si tiene menos de 70 → encuentra un enemigo y pierde 20 puntos.
|
||||
//Cueva
|
||||
// Si tiene 50 puntos de vida o más → consigue una espada.
|
||||
// Si tiene menos de 50 → debe escapar y pierde 10 puntos.
|
||||
//Castillo
|
||||
// Si tiene 80 puntos de vida o más → puede entrar.
|
||||
// Si tiene menos de 80 → el guardia no le permite entrar.
|
||||
//Si introduce otra opción:
|
||||
//"Camino no válido".
|
||||
//Al finalizar, mostrar los puntos de vida del jugador.
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
void main() {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
int vida = 100;
|
||||
|
||||
System.out.println("A donde quieres ir?");
|
||||
System.out.println("Bosque, cueva, castillo");
|
||||
String ubicacion = sc.next().toLowerCase();
|
||||
switch(ubicacion){
|
||||
case "bosque":
|
||||
if(vida >= 70){
|
||||
System.out.println("Te has encontrado un tesoro :-)");
|
||||
} else {
|
||||
System.out.println("Hay un enemigo bien poderoso y te quita 20 de vida");
|
||||
vida -= 20;
|
||||
}
|
||||
break;
|
||||
case "cueva":
|
||||
if(vida >= 50) {
|
||||
System.out.println("Has conseguido una espada!!");
|
||||
} else {
|
||||
System.out.println("Te ves obligado a huir y pierdes 10 de vida");
|
||||
vida -= 10;
|
||||
}
|
||||
break;
|
||||
case "castillo":
|
||||
if(vida >= 80) {
|
||||
System.out.println("Puedes entrar");
|
||||
} else {
|
||||
System.out.println("El guardia no te permite entrar");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
System.out.println("Camino inválido");
|
||||
}
|
||||
System.out.printf("Tienes %d puntos de vida\n", vida);
|
||||
sc.close();
|
||||
}
|
||||
Executable
+8
@@ -0,0 +1,8 @@
|
||||
// Diseña un algoritmo que visualice los 50 primeros números enteros.
|
||||
//
|
||||
void main() {
|
||||
int i = -1;
|
||||
while (++i < 50)
|
||||
System.out.print(i + ", ");
|
||||
System.out.println();
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// Diseñar un algoritmo que realice y muestre el producto de dos números enteros positivos
|
||||
// introducidos por teclado utilizando sumas sucesivas.
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
void main() {
|
||||
long result = 0;
|
||||
|
||||
Scanner sc = new Scanner(System.in);
|
||||
|
||||
System.out.println("Introduce el primer número: ");
|
||||
int n1 = sc.nextInt();
|
||||
|
||||
System.out.println("Introduce el segundo número: ");
|
||||
int n2 = sc.nextInt();
|
||||
|
||||
sc.close();
|
||||
|
||||
while(n2-- > 0){
|
||||
result += n1;
|
||||
}
|
||||
|
||||
System.out.printf("Su producto es: %d\n", result);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// Diseñar un algoritmo que realice la división de dos números enteros positivos introducidos
|
||||
// a través del teclado mediante restas sucesivas, mostrando el cociente y el resto
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
void main() {
|
||||
long cociente = 0, resto = 0;
|
||||
|
||||
Scanner sc = new Scanner(System.in);
|
||||
|
||||
System.out.println("Introduce el dividendo: ");
|
||||
int dividendo = sc.nextInt();
|
||||
|
||||
System.out.println("Introduce el divisor: ");
|
||||
int divisor = sc.nextInt();
|
||||
|
||||
sc.close();
|
||||
|
||||
while(dividendo >= divisor){
|
||||
dividendo -= divisor;
|
||||
cociente++;
|
||||
}
|
||||
resto = dividendo;
|
||||
|
||||
System.out.printf("El cociente es: %d\nEl resto es: %d\n", cociente, resto);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// Escribir un algoritmo que lea un número y escriba todos los números comprendidos entre
|
||||
// dicho número y la unidad en orden decreciente. (utilizando el bucle repetir-mientras)
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
void main() {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.println("Introduce un número: ");
|
||||
int input = sc.nextInt();
|
||||
sc.close();
|
||||
|
||||
do {
|
||||
System.out.printf("%d, ", input);
|
||||
} while (--input > 0);
|
||||
System.out.println();
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
// Diseñar un algoritmo que lee “N” caracteres y contabiliza el número de veces que se repiten
|
||||
// las vocales a, e, i, o, y u (utilizando el bucle repetir-mientras)
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
void main() {
|
||||
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: ");
|
||||
do {
|
||||
char c = sc.next().charAt(0);
|
||||
switch (c) {
|
||||
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("a: %d\ne: %d\ni: %d\no: %d\nu: %d\n", a, e, i, o, u);
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// Diseñar un algoritmo que lee “N” caracteres y contabiliza el número de veces que se repiten
|
||||
// las vocales a, e, i, o, y u (utilizando el bucle repetir-mientras)
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
void main() {
|
||||
long result = 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): ");
|
||||
for(int i = 0; i < n; i++){
|
||||
char c = sc.nextLine().charAt(0);
|
||||
switch (c) {
|
||||
case 'a', 'e', 'i', 'o', 'u':
|
||||
result++;
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
sc.close();
|
||||
|
||||
System.out.printf("Hay %d vocales\n", result);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// Escribir un algoritmo que lee un número entero positivo y seguidamente escribe el carácter
|
||||
// “*”, un número de veces igual al valor numérico leído. En aquellos casos en que el valor
|
||||
// leído no sea positivo se deberá escribir un único asterisco.
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
void main() {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.println("Introduce un número: ");
|
||||
int input = sc.nextInt();
|
||||
sc.close();
|
||||
do {
|
||||
System.out.print("*");
|
||||
} while(--input > 0);
|
||||
System.out.println();
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// Escribir un algoritmo que lee un número entero positivo y seguidamente escribe el carácter
|
||||
// “*”, un número de veces igual al valor numérico leído. En aquellos casos en que el valor
|
||||
// leído no sea positivo se deberá escribir un único asterisco.
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
void main() {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.println("Introduce un número: ");
|
||||
int input = sc.nextInt();
|
||||
sc.close();
|
||||
for (int i = 0; i < input; i++) {
|
||||
System.out.print("*");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// Diseñar un algoritmo que verifique que la entrada de un dato es correcta. Un dato es
|
||||
// correcto si la edad introducida de una persona está entre 5 y 25 años ambos inclusive,y no
|
||||
// es una edad par.
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
void main() {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.println("Introduce tu edad: ");
|
||||
int input = sc.nextInt();
|
||||
sc.close();
|
||||
|
||||
if (input < 5 || input > 25 || (input & 1) == 0) {
|
||||
System.out.println("Edad inválida");
|
||||
} else {
|
||||
System.out.println("Edad válida");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// Diseña un algoritmo que muestre los números comprendidos entre el 1 y 50
|
||||
|
||||
void main() {
|
||||
int i = 1;
|
||||
do {
|
||||
System.out.printf("%d, ", i);
|
||||
} while (++i <= 50);
|
||||
System.out.println();
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// Diseña un algoritmo que muestre los números comprendidos entre el 1 y 50
|
||||
|
||||
void main() {
|
||||
for (int i = 0; i <= 50; i++) {
|
||||
System.out.printf("%d, ", i);
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// Diseña un algoritmo que muestre los números comprendidos entre el 1 y un número
|
||||
// introducido por teclado en orden descendente
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
void main() {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.println("Introduce algo: ");
|
||||
int input = sc.nextInt();
|
||||
sc.close();
|
||||
|
||||
do {
|
||||
System.out.printf("%d, ", input);
|
||||
} while (--input > 0);
|
||||
System.out.println();
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// Diseña un algoritmo que muestre los números comprendidos entre el 1 y un número
|
||||
// introducido por teclado en orden descendente
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
void main() {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.println("Introduce algo: ");
|
||||
int input = sc.nextInt();
|
||||
sc.close();
|
||||
|
||||
for(int i = input; i > 0; i--) {
|
||||
System.out.printf("%d, ", i);
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
// Diseña un algoritmo que calcule y muestre el factorial de un número entero positivo dado
|
||||
// por teclado
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
void main() {
|
||||
System.out.println("Calcular factorial de: ");
|
||||
|
||||
Scanner sc = new Scanner(System.in);
|
||||
int n = sc.nextInt();
|
||||
sc.close();
|
||||
|
||||
long resultado = 1;
|
||||
// En este caso no funciona con do..while() porque 0! es 1 no 0
|
||||
if (n > 0) {
|
||||
do {
|
||||
resultado *= n--;
|
||||
} while (n > 0);
|
||||
}
|
||||
|
||||
System.out.printf("El resultado es: %d\n", resultado);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// Diseñar un algoritmo correspondiente a un programa que lee un número entero positivo y
|
||||
// determina el número de dígitos decimales necesarios para la representación de dicho valor
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
void main() {
|
||||
System.out.println("Introduce un número positivo: ");
|
||||
|
||||
Scanner sc = new Scanner(System.in);
|
||||
int n = sc.nextInt();
|
||||
sc.close();
|
||||
|
||||
int resultado = 0;
|
||||
do {
|
||||
n /= 10;
|
||||
resultado++;
|
||||
} while (n > 0);
|
||||
|
||||
System.out.printf("El resultado es %d\n", resultado);
|
||||
}
|
||||
Executable
+6
@@ -0,0 +1,6 @@
|
||||
// Diseña un algoritmo que visualice los 50 primeros números enteros.
|
||||
void main() {
|
||||
for(int i = 0; i < 50; i++)
|
||||
System.out.print(i + ", ");
|
||||
System.out.println();
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
// Diseña un algoritmo que sume los números comprendidos entre el 1 y un número introducido
|
||||
// por el usuario a través del teclado. Si el número introducido es <1 la suma será 0.
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
void main() {
|
||||
System.out.println("Introduce un número:");
|
||||
long resultado = 0;
|
||||
|
||||
Scanner sc = new Scanner(System.in);
|
||||
int n = sc.nextInt();
|
||||
sc.close();
|
||||
|
||||
if (n >= 1) {
|
||||
int i = 0;
|
||||
while (++i <= n)
|
||||
resultado += i;
|
||||
} else
|
||||
resultado = 0;
|
||||
|
||||
System.out.printf("El resultado es %d\n", resultado);
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// Diseña un algoritmo que lee cinco valores numéricos y calcula su producto
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
void main() {
|
||||
System.out.println("Introduce 5 valores numéricos");
|
||||
int resultado = 1;
|
||||
Scanner sc = new Scanner(System.in);
|
||||
for (int i = 0; i < 5; i++) {
|
||||
resultado *= sc.nextInt();
|
||||
}
|
||||
sc.close();
|
||||
System.out.printf("El resultado es: %d\n", resultado);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// Diseña un algoritmo que calcule y muestre la suma de una serie de números
|
||||
// introducidos por teclado, siendo introducido por teclado el número de valores que hay
|
||||
// que leer.
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
void main() {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.println("Cuantos valores vas a introducir?");
|
||||
int n = sc.nextInt();
|
||||
System.out.printf("Introduce %d valores numéricos\n", n);
|
||||
int resultado = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
resultado += sc.nextInt();
|
||||
}
|
||||
sc.close();
|
||||
System.out.printf("El resultado es: %d\n", resultado);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// Diseña un algoritmo que sume los números comprendidos entre el 1 y un número introducido
|
||||
// por el usuario a través del teclado. Si el número introducido es <1 la suma será 0.
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
void main() {
|
||||
System.out.println("Introduce un número:");
|
||||
long resultado = 0;
|
||||
|
||||
Scanner sc = new Scanner(System.in);
|
||||
int n = sc.nextInt();
|
||||
sc.close();
|
||||
|
||||
if (n >= 1) {
|
||||
for(int i = 0; i <= n; i++)
|
||||
resultado += i;
|
||||
} else
|
||||
resultado = 0;
|
||||
|
||||
System.out.printf("El resultado es %d\n", resultado);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// Diseña un algoritmo que muestre los números comprendidos entre dos números
|
||||
// introducidos desde el teclado en orden ascendente
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
void main() {
|
||||
System.out.println("Introduce dos números:");
|
||||
|
||||
Scanner sc = new Scanner(System.in);
|
||||
int n1 = sc.nextInt();
|
||||
int n2 = sc.nextInt();
|
||||
sc.close();
|
||||
|
||||
int mayor = Math.max(n1, n2);
|
||||
int menor = Math.min(n1, n2);
|
||||
|
||||
while(menor <= mayor)
|
||||
System.out.printf("%d, ", menor++);
|
||||
System.out.println();
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// Diseño de un algoritmo que sume todos aquellos números leídos mientras no sean negativos,
|
||||
// al introducirse un número negativo, muestra la suma y acaba el programa
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
void main() {
|
||||
int resultado = 0;
|
||||
Scanner sc = new Scanner(System.in);
|
||||
|
||||
while (true) {
|
||||
System.out.print("Introduce un número: ");
|
||||
int n = sc.nextInt();
|
||||
if (n < 0)
|
||||
break;
|
||||
resultado += n;
|
||||
}
|
||||
|
||||
sc.close();
|
||||
System.out.printf("El resultado es: %d\n", resultado);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// Diseña un algoritmo que calcule y muestre el factorial de un número entero positivo dado
|
||||
// por teclado
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
void main() {
|
||||
System.out.println("Calcular factorial de: ");
|
||||
|
||||
Scanner sc = new Scanner(System.in);
|
||||
int n = sc.nextInt();
|
||||
sc.close();
|
||||
|
||||
long resultado = 1;
|
||||
while (n > 0)
|
||||
resultado *= n--;
|
||||
|
||||
System.out.printf("El resultado es: %d\n", resultado);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// Diseñar un algoritmo correspondiente a un programa que lee un número entero positivo y
|
||||
// determina el número de dígitos decimales necesarios para la representación de dicho valor
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
void main() {
|
||||
System.out.println("Introduce un número positivo: ");
|
||||
|
||||
Scanner sc = new Scanner(System.in);
|
||||
int n = sc.nextInt();
|
||||
sc.close();
|
||||
|
||||
int resultado = 0;
|
||||
while (n > 0) {
|
||||
n /= 10;
|
||||
resultado++;
|
||||
}
|
||||
|
||||
System.out.printf("El resultado es %d\n", resultado);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
// Diseña un algoritmo que lea 100 números y cuente los positivos, negativos y nulos y muestre
|
||||
// los totales
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
void main() {
|
||||
int positivos = 0, negativos = 0, nulos = 0;
|
||||
System.out.println("Introduce 100 numeros: ");
|
||||
Scanner sc = new Scanner(System.in);
|
||||
int i = 0;
|
||||
while (i++ < 100) {
|
||||
int n = sc.nextInt();
|
||||
if (n > 0)
|
||||
positivos++;
|
||||
else if (n < 0) {
|
||||
negativos++;
|
||||
} else
|
||||
nulos++;
|
||||
}
|
||||
sc.close();
|
||||
System.out.printf("positivos: %d\nnegativos: %d\nnulos: %d\n", positivos, negativos, nulos);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// Diseña un algoritmo que lea 100 números y cuente los positivos, negativos y nulos y muestre
|
||||
// los totales
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
void main() {
|
||||
int positivos = 0, negativos = 0, nulos = 0;
|
||||
System.out.println("Introduce 100 numeros: ");
|
||||
Scanner sc = new Scanner(System.in);
|
||||
for(int i = 0; i < 100; i++) {
|
||||
int n = sc.nextInt();
|
||||
if (n > 0)
|
||||
positivos++;
|
||||
else if (n < 0) {
|
||||
negativos++;
|
||||
} else
|
||||
nulos++;
|
||||
}
|
||||
sc.close();
|
||||
System.out.printf("positivos: %d\nnegativos: %d\nnulos: %d\n", positivos, negativos, nulos);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// Diseña un algoritmo que calcule la potencia de un número por productos sucesivos dada la
|
||||
// base y el exponente
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
void main() {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.println("Introduce la base: ");
|
||||
long base = sc.nextLong();
|
||||
System.out.println("Introduce el exponente: ");
|
||||
long exponente = sc.nextLong();
|
||||
sc.close();
|
||||
|
||||
long resultado = 1;
|
||||
while (exponente-- > 0)
|
||||
resultado *= base;
|
||||
|
||||
System.out.printf("resultado: %d\n", resultado);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// Diseñar un algoritmo que calcule y muestre la suma de números introducidos por teclado
|
||||
// hasta que dicha suma sea mayor que 1000
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
void main() {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
long suma = 0;
|
||||
while (suma < 1000) {
|
||||
System.out.println("Introduce un número");
|
||||
suma += sc.nextLong();
|
||||
System.out.printf("suma: %d\n", suma);
|
||||
}
|
||||
sc.close();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
// Calculadora. Leer dos números y una operación (+ */ ) y mostrar el resultado
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
void main() {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
int result, n1, n2;
|
||||
char op = 0;
|
||||
System.out.print("Pon una operación: ");
|
||||
n1 = sc.nextInt();
|
||||
op = sc.next().charAt(0);
|
||||
n2 = sc.nextInt();
|
||||
switch (op) {
|
||||
case '+':
|
||||
result = n1 + n2;
|
||||
break;
|
||||
case '-':
|
||||
result = n1 - n2;
|
||||
break;
|
||||
case '*':
|
||||
result = n1 * n2;
|
||||
break;
|
||||
case '/':
|
||||
if (n2 == 0){
|
||||
System.out.println("Imposible dividir entre 0");
|
||||
return;
|
||||
}
|
||||
result = n1 / n2;
|
||||
break;
|
||||
default:
|
||||
System.out.println("Operador inválido");
|
||||
sc.close();
|
||||
return;
|
||||
}
|
||||
System.out.println("El resultado es " + result);
|
||||
sc.close();
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
void main() {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
|
||||
System.out.println("Día de la semana:");
|
||||
String dia = sc.next().toLowerCase();
|
||||
|
||||
switch(dia){
|
||||
case "lunes", "martes", "miércoles", "jueves", "viernes":
|
||||
System.out.println("Es lectivo");
|
||||
break;
|
||||
case "sábado", "domingo":
|
||||
System.out.println("Es festivo");
|
||||
break;
|
||||
default:
|
||||
System.out.println("ERROR: Día inválido");
|
||||
}
|
||||
|
||||
sc.close();
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
void main() {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
|
||||
System.out.println("Introduce un número:");
|
||||
int n = sc.nextInt();
|
||||
|
||||
switch(n){
|
||||
case 0:
|
||||
System.out.println("Cero");
|
||||
break;
|
||||
case 1:
|
||||
System.out.println("Uno");
|
||||
break;
|
||||
case 2:
|
||||
System.out.println("Dos");
|
||||
break;
|
||||
case 3:
|
||||
System.out.println("Tres");
|
||||
break;
|
||||
case 4:
|
||||
System.out.println("Cuatro");
|
||||
break;
|
||||
case 5:
|
||||
System.out.println("Cinco");
|
||||
break;
|
||||
case 6:
|
||||
System.out.println("Seis");
|
||||
break;
|
||||
case 7:
|
||||
System.out.println("Siete");
|
||||
break;
|
||||
case 8:
|
||||
System.out.println("Ocho");
|
||||
break;
|
||||
case 9:
|
||||
System.out.println("Nueve");
|
||||
break;
|
||||
default:
|
||||
System.out.println("Número inválido (0-9)");
|
||||
}
|
||||
|
||||
sc.close();
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
// Diseña un algoritmo, que al introducir una calificación entre 0 y 10
|
||||
//(consideramos que las notas son valores enteros), nos devuelva la calificación
|
||||
//numérica según la siguiente tabla:
|
||||
// 0<=nota<3: MD
|
||||
// 3<=nota<5: INS
|
||||
// 5<=nota<7: BIEN
|
||||
// 7<=nota<9: NOT
|
||||
// 9<=nota<=10: SOB
|
||||
// cualquier otro valor de nota mostrará un mensaje de error.
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
void main() {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.println("Cuanto has sacado?");
|
||||
double nota = sc.nextDouble();
|
||||
|
||||
if(nota < 0 || nota > 10){
|
||||
System.out.println("ERROR: Nota inválida");
|
||||
return;
|
||||
}
|
||||
|
||||
if(nota < 3.00){
|
||||
System.out.println("MD");
|
||||
} else if (nota < 5.00){
|
||||
System.out.println("INS");
|
||||
} else if (nota < 7.00){
|
||||
System.out.println("BIEN");
|
||||
} else if (nota < 9.00){
|
||||
System.out.println("NOT");
|
||||
} else {
|
||||
System.out.println("SOB");
|
||||
}
|
||||
|
||||
sc.close();
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// Pedir por teclado el día de la semana y escribir si es lectivo o festivo(fin de
|
||||
//semana)
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
void main() {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
final String dias[] = {"Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado", "Domingo"};
|
||||
|
||||
System.out.println("Día de la semana:");
|
||||
String dia = sc.next();
|
||||
for(int i = 0; i < dias.length; i++){
|
||||
if(dias[i].equalsIgnoreCase(dia)){
|
||||
System.out.printf("Los %s son %s\n", dia, (i < 5) ? "lectivos" : "fines de semana");
|
||||
sc.close();
|
||||
return;
|
||||
}
|
||||
}
|
||||
System.out.println("ERROR: Día inválido");
|
||||
sc.close();
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
// Escribe un programa que solicite tres valores enteros con el día, mes y año de
|
||||
//una fecha e indique si se trata de valores válidos para una fecha. (tener en cuenta
|
||||
//el número de días de cada mes)(el año será válido si es >=1970 y menor que 3000)
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
void main() {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
|
||||
System.out.println("Introduce dia mes y año:");
|
||||
int dia = sc.nextInt();
|
||||
int mes = sc.nextInt();
|
||||
int ano = sc.nextInt();
|
||||
|
||||
if (!(ano >= 1970 && ano < 3000)) {
|
||||
System.out.println("Fecha inválida");
|
||||
sc.close();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(mes > 0 && mes <= 12)){
|
||||
System.out.println("Fecha inválida");
|
||||
sc.close();
|
||||
return;
|
||||
}
|
||||
|
||||
boolean bisiesto = (ano % 4 == 0 && ano % 100 != 0) || (ano % 400 == 0);
|
||||
final int diasMes[] = {
|
||||
31, // Enero
|
||||
(bisiesto) ? 29 : 28, // Febrero
|
||||
31, // Marzo
|
||||
30, // Abril
|
||||
31, // Mayo
|
||||
30, // Junio
|
||||
31, // Julio
|
||||
31, // Agosto
|
||||
30, // Septiembre
|
||||
31, // Octubre
|
||||
30, // Noviembre
|
||||
31}; // Diciembre
|
||||
|
||||
int diasMaximo = diasMes[mes-1];
|
||||
|
||||
if (!(dia > 0 && dia <= diasMaximo)) {
|
||||
System.out.println("Fecha inválida");
|
||||
sc.close();
|
||||
return;
|
||||
}
|
||||
System.out.println("Fecha válida");
|
||||
sc.close();
|
||||
}
|
||||
@@ -21,6 +21,11 @@ void main() {
|
||||
result = n1 * n2;
|
||||
break;
|
||||
case '/':
|
||||
if (n2 == 0){
|
||||
System.out.println("Imposible dividir entre 0");
|
||||
sc.close();
|
||||
return;
|
||||
}
|
||||
result = n1 / n2;
|
||||
break;
|
||||
default:
|
||||
|
||||
+10
-10
@@ -1,8 +1,8 @@
|
||||
// Los trabajadores de una fábrica de automóviles tienen tres turnos: mañana, noche y festivo. Se desea
|
||||
//calcular el sueldo semanal a razón de 5 díasde trabajo por semana y la siguiente tarifa:
|
||||
// a. 600 pts/h mañana
|
||||
// b. 800 pts/h noche
|
||||
// c. 1000 pts/h festivo
|
||||
// a. 15 €/h mañana
|
||||
// b. 20 €/h noche
|
||||
// c. 25 €/h festivo
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
@@ -10,13 +10,13 @@ void main() {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.println("En que turno trabajas?");
|
||||
String turno = sc.nextLine();
|
||||
int puntos;
|
||||
int euros;
|
||||
if (turno.equalsIgnoreCase("mañana")) {
|
||||
puntos = 600;
|
||||
euros = 15;
|
||||
} else if (turno.equalsIgnoreCase("noche")) {
|
||||
puntos = 800;
|
||||
euros = 20;
|
||||
} else if (turno.equalsIgnoreCase("festivo")) {
|
||||
puntos = 1000;
|
||||
euros = 25;
|
||||
} else {
|
||||
System.out.println("Turno invalido");
|
||||
sc.close();
|
||||
@@ -24,8 +24,8 @@ void main() {
|
||||
}
|
||||
System.out.println("Cuantas horas trabajas al dia?");
|
||||
int horas = sc.nextInt();
|
||||
puntos *= horas; // Horas que trabaja
|
||||
puntos *= 5; // Días laborables
|
||||
System.out.printf("Ganas %d puntos cada semana\n", puntos);
|
||||
euros *= horas; // Horas que trabaja
|
||||
euros *= 5; // 5 días/semana
|
||||
System.out.printf("Ganas %d€ a la semana\n", euros);
|
||||
sc.close();
|
||||
}
|
||||
+13
-7
@@ -11,14 +11,20 @@ void main() {
|
||||
|
||||
System.out.println("Cuantos euros quieres sacar?");
|
||||
int centimos = (int) Math.round(sc.nextDouble() * 100);
|
||||
|
||||
for (int i = 0; centimos != 0 && i < monedas.length;) {
|
||||
int contador = 0;
|
||||
for (int i = 0; i < monedas.length;) {
|
||||
int moneda = monedas[i];
|
||||
if (centimos >= moneda) {
|
||||
System.out.printf("Dar moneda de %.2f€\n", moneda / 100.0);
|
||||
centimos -= moneda;
|
||||
} else
|
||||
i++;
|
||||
if (centimos < moneda) {
|
||||
if (contador > 0){
|
||||
System.out.printf("Dar %d moneda%s de %.2f€\n", contador, (contador > 1) ? "s" : "", moneda / 100.0);
|
||||
contador = 0;
|
||||
}
|
||||
i++;
|
||||
} else{
|
||||
centimos -= moneda;
|
||||
contador++;
|
||||
}
|
||||
}
|
||||
|
||||
sc.close();
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import java.util.Scanner;
|
||||
|
||||
void main() {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
int mayor = 0;
|
||||
int mayor = Integer.MIN_VALUE;
|
||||
System.out.print("Escribe tres numeros: ");
|
||||
for (int i = 0; i < 3; i++) {
|
||||
int n = sc.nextInt();
|
||||
|
||||
@@ -11,6 +11,11 @@ void main() {
|
||||
h = sc.nextInt();
|
||||
m = sc.nextInt();
|
||||
s = sc.nextInt();
|
||||
if (h >= 24 || m >= 60 || s >= 60){
|
||||
System.out.println("Error: Hora inválida");
|
||||
sc.close();
|
||||
return;
|
||||
}
|
||||
s++;
|
||||
// Espagueti máximo xd
|
||||
if(s > 59){
|
||||
@@ -20,7 +25,6 @@ void main() {
|
||||
m = 0;
|
||||
h++;
|
||||
if(h > 23){
|
||||
m = 0;
|
||||
h = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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("--------------");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
Reference in new issue
Block a user