存储用户输入到一个数组 [英] Storing User Input into an Array

查看:97
本文介绍了存储用户输入到一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关大学的任务,我需要制作一个Java系统中,在港船舶停靠。港口有3个码头,每个码头包含10个空格停靠的船只。然而,空间是不同的尺寸,以适应不同大小的船只。小型船舶可以容纳大中小型空间,中型船舶可以适合大中型场所等。

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.

我需要用户输入并存储到一个2维数组。用户输入要求船舶的名称和大小,然后我需要将其与数组中的'码头空间比较用户,但我现在不知道如何做到这一点。在一些伪$ C $的c那么它会是这样:

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!

任何帮助将是辉煌的,

先谢谢了!

推荐答案

首先,决定如何重新present泊位大小。我建议使用简单起见的 INT 数据类型。小泊位可能是大小 1 ,中 2 和大 3

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" };

在要求用户提供一个船的大小,如果你present选择为小型的媒体,3大输入1,2,你可以使用读取输入

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;
    }
}

或者,如果你有predefined泊位的大小,你可以创建码头这样的事情吧。

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));
}

这应该足以让你开始。

这篇关于存储用户输入到一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆