-
Notifications
You must be signed in to change notification settings - Fork 0
/
Actividad2.java
49 lines (44 loc) · 1.69 KB
/
Actividad2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.util.Scanner;
public class Actividad2 {
// Métdo para ingresar un número decimal por parte del usuario
public double ingresoNumero(Scanner scanner) {
double numero = 0;
try {
System.out.println("Ingrese su número racional:");
numero = scanner.nextDouble(); // Cambiado de nextInt() a nextDouble() para admitir decimales
} catch (Exception e) {
System.out.println("El número ingresado no es válido: " + e.getMessage());
}
return numero;
}
// Métdo para redondear el número utilizando el método Math.round()
public double rndNumero(double numero) {
double rndN = 0;
try {
rndN = Math.round(numero);
} catch (RuntimeException e) {
System.out.println("No se pudo hacer la transformación del número: " + e.getMessage());
}
return rndN;
}
// Métdo para mostrar el número redondeado
public void mostrarNumero(double rndN, double numero) {
try {
System.out.println("El valor redondeado de: " + numero + " es: " + rndN);
} catch (Exception e) {
System.out.println("Error al mostrar el resultado: " + e.getMessage());
}
}
// Métdo principal que coordina la ejecución de la actividad
public void procesarActividad2() {
Scanner scanner = new Scanner(System.in);
try {
double numero = ingresoNumero(scanner);
double rndN = rndNumero(numero);
mostrarNumero(rndN, numero);
} finally {
// Cerramos el scanner en el finally para asegurar que siempre se cierre
scanner.close();
}
}
}