简体   繁体   中英

I enter multiple objects in an arraylist but it only prints one JAVA

I'm trying to make a script where I can enter students with their last name and grade, teachers, print the students with their grades, and print the teachers. I created an object "Estudiante" and I was entering it in an arraylist but at the time of printing the arraylist only prints one element

import java.util.*;

public class tarea1 {   
public static void main(String[] args) {
    
ArrayList<Estudiante> Estudiantes = new ArrayList();
ArrayList<Profesor> Profesores = new ArrayList();
Scanner sc = new Scanner(System.in);

System.out.println("1.Ingresar estudiantes \n2.Ingresar Maestros \n3.Ver alumnos con sus notas \n4.Ver maestros \n5.Salir");    

while(true){
    System.out.println("Elija el número de la opción deseada"); 

    try {
        int opcion = sc.nextInt();
        switch(opcion) {
        case 1:
            while(true) {
                Estudiante aux = new Estudiante();
                aux.ingresar_estudiante();
                Estudiantes.add(aux);
                
                sc.nextLine();
                System.out.println("Si desea seguir ingreseando resultados escriba Y, sino escriba cualquier otra letra");
                
                String continuar = sc.nextLine();
                
                
                if(continuar.equalsIgnoreCase("Y")) {
                    continue;               
                }
                else {
                    break;
                }
            }
continue;

in this way I print them

    case 3:
            for(Estudiante e:Estudiantes) {
                System.out.println(e.ver_estudiantes());
                break;
            }

And this is the class


import java.util.*;

public  class Estudiante {
    
     private String nombre;
     private String apellido;
     private double nota;
    
    public Estudiante() {       
    }
    
    
    
     public void ingresar_estudiante() {
        
        System.out.println("Ingresar estudiantes");
        
                
        
            Scanner sc = new Scanner(System.in);
            
            System.out.println("Ingresa el nomnre del estudiante: ");
            nombre = sc.nextLine();
            
            System.out.println("Ingresa el apellido del estudiante: ");
            apellido = sc.nextLine();
            
            System.out.println("Ingresa la nota separada de un punto: ");
            nota = sc.nextDouble();
                        
            sc.nextLine();
            
            
            
            
        }

    
    public String ver_estudiantes() {
        return "El nombre del estudiante es " + nombre +" " + apellido + " y su nota es de " + nota;
    }
}

There is a "break" in case 3 in switch-case. It breaks the "for loop" after the first iteration.

 case 3:
        for(Estudiante e:Estudiantes) {
            System.out.println(e.ver_estudiantes());
         
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM