Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Scanner;
- public class Media {
- // Global Variables
- public static Scanner sc = new Scanner(System.in);
- // Main Class
- public static void main(String[] args) {
- boolean interruptor = false;
- do {
- Alumno nuevo = new Alumno();
- System.out.println("Inserte el nombre del alumno.");
- nuevo.nombre = sc.nextLine();
- System.out.println("Inserte las notas del alumno");
- double basura = 0;
- for(int i = 0;i < nuevo.notas.length;i++) {
- basura = sc.nextDouble();
- if(basura >= 0 && basura <= 10) {
- nuevo.notas[i] = basura;
- }
- else {
- System.out.println("Debe insertar un numero que este entre el 0 y el 10");
- i--;
- }
- }
- nuevo.media = Alumno.getMedia(nuevo.notas);
- nuevo.media = Math.rint(nuevo.media*100)/100;
- // Map (Array Asociativo)
- Map<String, Alumno> alumnos = new HashMap<>();
- alumnos.put(nuevo.nombre, nuevo);
- // We show the data from the array.
- System.out.println("----------------------------------\n" +
- "--------------ALUMNOS-------------\n" +
- "----------------------------------");
- for(String id : alumnos.keySet()) {
- Alumno alumn = alumnos.get(id);
- System.out.println("Alumno: " + alumn.nombre);
- System.out.print("Notas: ");
- for(int i = 0;i < alumn.notas.length;i++) {
- if(i < (alumn.notas.length - 1))
- System.out.print(alumn.notas[i] + ", ");
- else
- System.out.print(alumn.notas[i] + "\n");
- }
- System.out.println("Media: " + alumn.media);
- }
- // We ask if the user wants to add more students
- System.out.println("Desea anhadir mas alumnos?");
- String basuraString = sc.nextLine();
- String continuar = sc.nextLine();
- if(continuar.equalsIgnoreCase("si")) {
- interruptor = true;
- }
- }while(interruptor);
- }
- }
- // Sub Classes
- class Alumno{
- // Methods
- public static double getMedia(double[] notas){
- double media = 0;
- for(int i = 0;i< notas.length;i++) {
- media += notas[i];
- }
- media = media / notas.length;
- return media;
- }
- String nombre;
- int maxNotas = 3;
- double[] notas = new double[maxNotas];
- double media;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement