首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何填充依赖于变量的多维数组

如何填充依赖于变量的多维数组
EN

Stack Overflow用户
提问于 2014-11-11 23:47:17
回答 1查看 198关注 0票数 2

我正在做的程序是一个简单的运输程序。我遇到的困难是在某些变量中填充多维数组因式分解。

示例

320件物品需要用不同的盒子大小运至1个接收器。

  • XL可容纳50件物品
  • LG能容纳20件物品
  • MD可容纳5件物品
  • SM可容纳1件物品

使用到目前为止最少的框数。

代码

到目前为止这是我的密码。

代码语言:javascript
复制
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]);
    //    }
    //}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-12 01:13:10

更改calculateBoxes()的定义,以接受表示每个框的卷的数组(在您的示例中,这将是{50、20、5、1}):

代码语言:javascript
复制
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循环)是:

代码语言:javascript
复制
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];
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26876752

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档