简体   繁体   中英

How do I create a loop that can calculate the number of times a specific number can be fitted in another specific number?

I am creating a program that shall work like this; First the program reads the total volume of a transport (eg truck) in cubic meters from the keyboard. Next the program calculates how many bags that can be stored in the truck and displays this information.

I always want to use as many big bags as possible, meaning that I want to use as many of the biggest bags as possible, then when they can not fit anymore of the biggest bags I want to store as many of the middle size bags as possible and when they can not store anymore middle sized bags they use the smallest size bags to fill up whatever space is left.

This is how I am trying to solve this, but the greatest problem remains in the looping or logic part, I don't know how do I loop the things so that I prints out what I want.

    package volumecalc;

import java.util.*;

public class BagObject {

    Scanner input = new Scanner(System.in);
    //volume in cubic meter
    int sBag = 10 * 10 * 30;     //Size of the smallest bag
    int mBag = 50 * 100 * 40;    //Size of the middle bag
    int bBag = 100 * 200 * 50;   //Size of the biggest bag
    int transVol;

    public void bag() {
        System.out.println("Enter the current volume of your transport: ");
        transVol = input.nextInt();

        if (transVol > bBag) {
            //Probaly here or more places, I need help
        } else if (transVol < sBag) {
            //Probaly here or more places, I need help
        }

    }

}

Main class:

    package volumecalc;

import java.util.*;

public class VolumeCalc {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("                THIS PROGRAM WILL CALCULATE THE SPACE (IN cubic centimeter) IN TRANSPORT:\n\n");
        BagObject bo = new BagObject();
        bo.bag();

    }

}

This is how I would do it:

int numbBag, nummBag, numsBag;    
int remainder;

numbBag = transvol / bBag;
remainder = transvol % bBag;

nummBag = remainder / mBag;
remainder %= mBag;

numsBag = remainder / sBag;

Assuming the transport has no specific shape.

If you want a loop that is reusable for any number of bags you can do this:

const int NUM_BAGS = 3;
//put all your bag sizes here
int[] bags = {bBag, mBag, sBag, /*other bags*/};//Array of bags from largest to smallest
int[] numBags = new int[NUM_BAGS];//Number of each bag in the transport
int remainder = transVol;

for(int varA; varA < NUM_BAGS; varA++)
{
    numBags[varA] = remainder / bags[varA];
    remainder %= bags[varA];
}

The number of bBags is in numBags[0], mBags in numBags[1] etc., in order from largest to smallest.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM