java - 如何根据变量填充多维数组

标签 java arrays multidimensional-array

我正在处理的程序是一个简单的运输程序。我遇到的困难是填充包含某些变量的多维数组。

例子

320 件商品需要使用不同的箱子尺寸运送到 1 个收件人。

  • XL 号可容纳 50 件元素
  • LG可容纳20件元素
  • MD可以装5个元素
  • SM可以容纳1个元素

使用迄今为止最少数量的盒子。

代码

到目前为止,这是我的代码。

import java.util.Scanner;

public class Shipping {
    public static void main(String [] args) {
        Scanner kbd = new Scanner(System.in);

        final int EXTRA_LARGE = 50;
        final int LARGE = 20;
        final int MEDIUM = 5;
        final int SMALL = 1;

        String sBusinessName = "";
        int iNumberOfGPS = 0;

        int iShipmentCount = 0;

        displayHeading(kbd);
        iShipmentCount = enterShipments(kbd);
        int[][] ai_NumberOfShipments = new int [iShipmentCount][4];
        String[] as_BusinessNames = new String [iShipmentCount];

        for (int iStepper = 0; iStepper < iShipmentCount; iStepper++) {
            sBusinessName = varifyBusinessName(kbd);
            as_BusinessNames[iStepper] = sBusinessName;
            iNumberOfGPS = varifyGPS(kbd);
            calculateBoxes(ai_NumberOfShipments[iStepper],iNumberOfGPS, EXTRA_LARGE, LARGE, MEDIUM, SMALL);
        }
        //showArray(as_BusinessNames);
    }

    public static void displayHeading(Scanner kbd) {
        System.out.println("Red River Electronics");
        System.out.println("Shipping System");
        System.out.println("---------------");

        return;
    }

    public static int enterShipments(Scanner kbd) {
        int iShipmentCount = 0;
        boolean bError = false;
        do {
            bError = false;
            System.out.print("How many shipments to enter? ");
            iShipmentCount = Integer.parseInt(kbd.nextLine());

            if (iShipmentCount < 1) {
                System.out.println("\n**Error** - Invalid number of shipments\n");
                bError = true;
            }
        } while (bError == true);

        return iShipmentCount;
    }

    public static String varifyBusinessName(Scanner kbd) {
        String sBusinessName = "", sValidName = "";
        do {
            System.out.print("Business Name: ");
            sBusinessName = kbd.nextLine();

            if (sBusinessName.length() == 0) {
                System.out.println("");
                System.out.println("**Error** - Name is required\n");
            } else if (sBusinessName.length() >= 1) {
                sValidName = sBusinessName;
            }
        } while (sValidName == "");

        return sValidName;
    }

    public static int varifyGPS(Scanner kbd) {
        int iCheckGPS = 0;
        int iValidGPS = 0;
        do {
            System.out.print("Enter the number of GPS receivers to ship: ");
            iCheckGPS = Integer.parseInt(kbd.nextLine());

            if (iCheckGPS < 1) {
                System.out.println("\n**Error** - Invalid number of shipments\n");
            } else if (iCheckGPS >= 1) {
                iValidGPS = iCheckGPS;
            }
        } while(iCheckGPS < 1);

        return iValidGPS;   
    }
    
    public static void calculateBoxes(int[] ai_ToFill, int iNumberOfGPS) {
        for (int iStepper = 0; iStepper < ai_ToFill.length; iStepper++)
    }

    //public static void showArray( String[] ai_ToShow) {
    //    for (int iStepper = 0; iStepper < ai_ToShow.length; iStepper++) {
    //        System.out.println("Integer at position " + iStepper + " is " + ai_ToShow[iStepper]);
    //    }
    //}
}

最佳答案

更改 calculateBoxes() 的定义,使其也采用一个数组来表示每个盒子的体积(在您的例子中,这将是 {50, 20, 5, 1}:

public static void calculateBoxes(int[] ai_ToFill, int[] boxVolumes, int iNumberOfGPS) {
    // for each box size
    for (int iStepper = 0; iStepper < ai_ToFill.length; iStepper++) {
        // while the remaining items to pack is greater than the current box size
        while(iNumberOfGPS >= boxVolumes[iStepper]) {
            // increment the current box type
            ai_ToFill[iStepper]++;
            // subtract the items that just got packed
            iNumberOfGPS -= boxVolumes[iStepper];
        }
    }
}

另一种计算方法(使用/和 % 而不是 while 循环)是:

public static void calculateBoxes(int[] ai_ToFill, int[] boxVolumes, int iNumberOfGPS) {
    // for each box size
    for (int iStepper = 0; iStepper < ai_ToFill.length; iStepper++) {
        if(iNumberOfGPS >= boxVolumes[iStepper]) {
            // calculate the number of boxes that could be filled by the items
            ai_ToFill[iStepper] = iNumberOfGPS/boxVolumes[iStepper];
            // reset the count of items to the remainder
            iNumberOfGPS = iNumberOfGPS%boxVolumes[iStepper];
        }
    }
}

关于java - 如何根据变量填充多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26876752/

相关文章:

java - Controller 中的 Spring MVC URL 映射 对于动态 URL

java - 递归检查字符串是否平衡

java - 字符串与对象#toString

c++ - 在数组中查找模式的最有效方法?

java - 带数组的多个输入

java - 代码在我们没有要求的情况下运行多次

python - 合并三个相同形状的 NumPy 数组

java - 并行处理: class file has wrong version 49. 0,应该是48.0

php - 如何使用php在json文件中查找数据

VBA:多维列表、数组、集合或字典的性能