Storing User Input into an Array [closed]

限于喜欢 提交于 2019-12-12 10:22:34

问题


For a university assignment I need to produce a java system for docking ships in a port. The port has 3 docks, each dock contains 10 spaces to dock ships. However the spaces are different sizes, to accommodate different size ships. Small ships can fit in small medium and large spaces and medium sized ships can fit into medium and large spaces etc.

I need to take the user input and store it into a 2 Dimensional Array. The user input asks the user for the name of the ship and the size then I need to compare it with the 'dock spaces' in the array, but I currently have no idea how to do this. In some for of pseudo code it would be like:

if (userInput == array[arrayValue]) {
//Store ship into array until the user selects to undock the ship
}

How do I get the system to compare the array values with the user input? If the user selects a small ship, but all the small ship spaces are taken how can I get the system to place it into a medium sized spot? Arrays are difficult!

Any help would be brilliant,

Thanks in Advance!


回答1:


First, decide how to represent the berth sizes. I suggest using the int data type for the sake of simplicity. Small berths could be size 1, medium 2 and large 3.

String[] sizes = { "X", "small", "medium", "large" };

When you ask the user for a boat size, if you present the choice "Enter 1 for small, 2 for medium, 3 for large", you could read the input using

int boatSize = scanner.nextInt();

Your 2D array would look something like this.

int dockCount = 3;
int berthCount = 10;
int[][] docks = new int[dockCount][berthCount];

Here's how you could fill that with randomly sized berths.

for (int dock = 0; dock < dockCount; dock++) {
    for (int berth = 0; berth < berthCount; berth++) {
        int size = (int)Math.floor(Math.random() * 3 + 1);
        docks[dock][berth] = size;
    }
}

Or if you have predefined berth sizes you could create the docks something like this instead.

int[][] docks = {
    { 1, 1, 1, 1, 2, 2, 2, 3, 3, 3 },
    { 1, 1, 1, 1, 1, 1, 2, 2, 3, 3 },
    { 1, 1, 1, 1, 1, 1, 1, 1, 2, 3 }
};

You'd also need a data structure to store whether each berth is full.

boolean[][] full = new boolean[dockCount][berthCount];

Assuming all of the above, to find an appropriate berth would go something like this.

int dock, berth, size;
for (size = boatSize; size <= 3; size++) {
    boolean found = false;
    for (dock = 0; dock < dockCount; dock++) {
        for (berth = 0; berth < berthCount; berth++) {
            if (!full[dock][berth]) {
                if (size == docks[dock][berth]) {
                    found = full[dock][berth] = true;
                    break;
                }
            }
        }
        if (found) break;
    }
    if (found) break;
}

if (size > 3) {
    System.out.println("No vacancy");
} else {
    System.out.println("Take the " + sizes[size]
        + " berth at dock " + (dock+1) + " space " + (space+1));
}

That should be enough to get you started.



来源:https://stackoverflow.com/questions/28817553/storing-user-input-into-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!