|
5 | 5 | //Fazer a divisão de 2 valores inteiros
|
6 | 6 | public class UncheckedException {
|
7 | 7 | public static void main(String[] args) {
|
8 |
| - String a = JOptionPane.showInputDialog("Numerador: "); |
9 |
| - String b = JOptionPane.showInputDialog("Denominador: "); |
10 | 8 |
|
11 |
| - double resultado = dividir(Integer.parseInt(a), Integer.parseInt(b)); |
12 |
| - System.out.println("Resultado: " + resultado); |
| 9 | + boolean continueLooping = true; |
| 10 | + do { |
| 11 | + String a = JOptionPane.showInputDialog("Numerador: "); |
| 12 | + String b = JOptionPane.showInputDialog("Denominador: "); |
| 13 | + |
| 14 | + try{ |
| 15 | + int resultado = dividir(Integer.parseInt(a), Integer.parseInt(b)); |
| 16 | + System.out.println("Resultado: " + resultado); |
| 17 | + continueLooping = false; |
| 18 | + } catch (NumberFormatException e) { |
| 19 | + e.printStackTrace(); |
| 20 | + JOptionPane.showMessageDialog(null, "Entrada inválida, informe um número inteiro! " + e.getMessage()); |
| 21 | + } catch (ArithmeticException e) { |
| 22 | + e.printStackTrace(); |
| 23 | + JOptionPane.showMessageDialog(null, "Impossível dividir um número por 0."); |
| 24 | + } |
| 25 | + finally { |
| 26 | + System.out.println("Chegou no finally!"); |
| 27 | + } |
| 28 | + } while(continueLooping); |
| 29 | + |
| 30 | + |
| 31 | + System.out.println("O código continua..."); |
13 | 32 | }
|
14 | 33 |
|
15 |
| - public static double dividir(int a, int b) { |
16 |
| - return (double) a / b; |
| 34 | + public static int dividir(int a, int b) { |
| 35 | + return a / b; |
17 | 36 | }
|
18 | 37 | }
|
0 commit comments