LexarG56

Generation

generate functionSun, 18 Jun 2023

Hacer un programa que solicite dos números y calcule la suma de los mismos, se debe capturar una posible excepción e informar sobre la misma.

import java.util.Scanner; import java.util.InputMismatchException; public class Main { public static void main(String[] args) { int a, b; Scanner in = new Scanner(System.in); try { System.out.println("Enter a number: "); a = in.nextInt(); System.out.println("Enter another number: "); b = in.nextInt(); System.out.println("Result: " + (a + b)); } catch (InputMismatchException e) { System.out.println("Invalid input!"); System.out.println("Sum not calculated!"); } } }

Questions about programming?Chat with your personal AI assistant