Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

158
Vistas
How can I fix this priority-queue code?

My code is basically like an elevator using dynamic queues. I want to imitate the way a elevator works using code. I set it up according to user input:

  • How many floors are in the building?
  • What floor the user is trying to reach?

After that, I check the numbers to make sure the floor number is in the range form 1 and number of floors. I put the numbers in a queue and when another number is added, I put the initial number in a dynamic queue.

I want to print all the numbers out that are passed on the way to the destination floor and I'm stuck right here. I want to print out the queue so I can see if it is working but I don't know how. I have to implement dynamic queue. So there is no getting rid of it.

import java.util.*;

public class ElevatorsCopy {

public static void main(String[] args) {

    Scanner floors = new Scanner(System.in);

    int floorMax;
    int floorNum;
    int n = 0;
    String floorCount = " ";

    System.out.println("How many floors are in the buiding");
    floorMax = floors.nextInt();

    System.out.println("There are " + floorMax + " floors in this building");

    System.out.println("Which floor would you like to go to?");

    floorNum = floors.nextInt();

    System.out.println("Going to the " + floorNum + " floor.");

    Queue<Integer> destFloor = new LinkedList<Integer>();

    PriorityQueue<Integer> priorityFloor = new PriorityQueue<Integer>();

    for ( int i =0 ; i <= floorMax; i++)
    {
        destFloor.add(i);
        for(int j =0; j <= floorMax; j++)
        {
            if(j <= floorMax)
            {
                destFloor.add(j);
                priorityFloor.add(i);
            }
            else{
                System.out.println("That floor does not exist.");
            }
        }
    }       
  }
}
over 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda