Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!