/ CalcularNota.java
CalcularNota.java
1 2 //Escribe un programa que lea una nota numérica entre 0 y 10 y muestre si el resultado es: insu- ficiente (menos de 5), suficiente (entre 5 y 6), bien(entre 6 y 7), notable(entre 7 y 9), excelente (entre 9 y 10) o matrícula de honor (10) 3 import java.util.Scanner; 4 5 public class CalcularNota { 6 public static void main(String[] args) { 7 Scanner sc = new Scanner(System.in); 8 9 System.out.println("=== CALCULADORA DE NOTAS ===\n"); 10 11 boolean continuar = true; 12 while (continuar) { 13 int nota = leerNota(sc); 14 15 if (nota < 5) { 16 System.out.println("Insuficiente"); 17 } else if (nota < 6) { 18 System.out.println("Suficiente"); 19 } else if (nota < 7) { 20 System.out.println("Bien"); 21 } else if (nota < 9) { 22 System.out.println("Notable"); 23 } else if (nota < 10) { 24 System.out.println("Excelente"); 25 } else { 26 System.out.println("Matrícula de honor"); 27 } 28 29 // Preguntar si continuar 30 System.out.print("\n¿Calcular otra nota? (1 = Sí, 0 = No): "); 31 int respuesta = leerNumero(sc); 32 continuar = (respuesta != 0); // boolean check 33 System.out.println(); 34 } 35 36 sc.close(); 37 System.out.println("FIN."); 38 } 39 40 public static int leerNota(Scanner sc) { 41 int nota = -1; 42 boolean valorValido = false; 43 44 while (!valorValido) { 45 try { 46 System.out.print("Introduce nota (0-10): "); 47 nota = sc.nextInt(); 48 sc.nextLine(); 49 50 if (nota >= 0 && nota <= 10) { 51 valorValido = true; 52 } else { 53 System.err.println("❌ La nota debe estar entre 0 y 10."); 54 } 55 56 } catch (Exception e) { 57 System.err.println("❌ Debe ser un número entre 0 y 10."); 58 sc.nextLine(); 59 } 60 } 61 62 return nota; 63 } 64 65 public static int leerNumero(Scanner sc) { 66 int numero = 0; 67 boolean valorValido = false; 68 69 while (!valorValido) { 70 try { 71 numero = sc.nextInt(); 72 sc.nextLine(); 73 74 if (numero > 1 || numero < 0) { 75 System.err.println("❌ Debes pulsar: 1 = Sí, 0 = No. "); 76 } else { 77 valorValido = true; 78 } 79 } catch (Exception e) { 80 sc.nextLine(); 81 numero = 0; // Por defecto, no continuar 82 } 83 } 84 return numero; 85 } 86 }