-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdivisionPorCero.java
More file actions
33 lines (24 loc) · 1.07 KB
/
divisionPorCero.java
File metadata and controls
33 lines (24 loc) · 1.07 KB
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
import java.util.Scanner;
public class divisionPorCero {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
System.out.print("Introduce el primer número: ");
double numero1 = scanner.nextDouble();
System.out.print("Introduce el segundo número: ");
double numero2 = scanner.nextDouble();
if (numero2 == 0) {
throw new ArithmeticException("No se puede dividir entre cero.");
}
double resultado = numero1 / numero2;
System.out.println("El resultado de la división es: " + resultado);
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
} catch (Exception e) {
System.out.println("Ha ocurrido un error inesperado: " + e.getMessage());
} finally {
System.out.println("Programa finalizado.");
scanner.close();
}
}
}