Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

162
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda