public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int numero, suma;
char opcion = 's';
do {
try {
System.out.println("Calcula a serie (suma) 1+2+3+...+N dado N.");
System.out.println("Introduza N: ");
numero = sc.nextInt();
suma = 0;
for (int i = 1; i <= numero; i++) {
suma += i;
}
System.out.println("A serie (suma = ): " + suma);
sc.nextLine(); // le o retorno de carro pendente
do {
System.out.println("Desexa continuar [s|n]?");
opcion = sc.nextLine().charAt(0);
if(opcion != 's' && opcion != 'n'){
System.out.println("Carácter erroneo, introduzca s/n");
}
} while (opcion != 's' && opcion != 'n');
} catch (InputMismatchException e) {
System.out.println("Numero non válido");
sc.nextLine();
} catch (IndexOutOfBoundsException e) {
System.out.println("Opción incorrecta");
sc.nextLine();
}
} while (opcion == 's');
sc.close();
}