Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4.  
  5. public class Media {
  6. // Global Variables
  7. public static Scanner sc = new Scanner(System.in);
  8.  
  9. // Main Class
  10. public static void main(String[] args) {
  11. boolean interruptor = false;
  12. do {
  13. Alumno nuevo = new Alumno();
  14. System.out.println("Inserte el nombre del alumno.");
  15. nuevo.nombre = sc.nextLine();
  16. System.out.println("Inserte las notas del alumno");
  17. double basura = 0;
  18. for(int i = 0;i < nuevo.notas.length;i++) {
  19. basura = sc.nextDouble();
  20. if(basura >= 0 && basura <= 10) {
  21. nuevo.notas[i] = basura;
  22. }
  23. else {
  24. System.out.println("Debe insertar un numero que este entre el 0 y el 10");
  25. i--;
  26. }
  27. }
  28.  
  29. nuevo.media = Alumno.getMedia(nuevo.notas);
  30. nuevo.media = Math.rint(nuevo.media*100)/100;
  31.  
  32. // Map (Array Asociativo)
  33. Map<String, Alumno> alumnos = new HashMap<>();
  34. alumnos.put(nuevo.nombre, nuevo);
  35.  
  36. // We show the data from the array.
  37. System.out.println("----------------------------------\n" +
  38. "--------------ALUMNOS-------------\n" +
  39. "----------------------------------");
  40. for(String id : alumnos.keySet()) {
  41. Alumno alumn = alumnos.get(id);
  42. System.out.println("Alumno: " + alumn.nombre);
  43. System.out.print("Notas: ");
  44. for(int i = 0;i < alumn.notas.length;i++) {
  45. if(i < (alumn.notas.length - 1))
  46. System.out.print(alumn.notas[i] + ", ");
  47. else
  48. System.out.print(alumn.notas[i] + "\n");
  49. }
  50. System.out.println("Media: " + alumn.media);
  51. }
  52.  
  53. // We ask if the user wants to add more students
  54. System.out.println("Desea anhadir mas alumnos?");
  55. String basuraString = sc.nextLine();
  56. String continuar = sc.nextLine();
  57. if(continuar.equalsIgnoreCase("si")) {
  58. interruptor = true;
  59. }
  60. }while(interruptor);
  61.  
  62. }
  63. }
  64.  
  65. // Sub Classes
  66. class Alumno{
  67. // Methods
  68. public static double getMedia(double[] notas){
  69. double media = 0;
  70. for(int i = 0;i< notas.length;i++) {
  71. media += notas[i];
  72. }
  73. media = media / notas.length;
  74. return media;
  75. }
  76.  
  77. String nombre;
  78. int maxNotas = 3;
  79. double[] notas = new double[maxNotas];
  80. double media;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement