0

我正在练习出书,自学。这让我大吃一惊。如果有人可以帮助我?

如何减去第一、第二和第三袋的金额以及收集袋的总数?

该项目要求这些标准 > 分配详细信息 该公司仅销售 2 磅袋装的咖啡。每个袋子的价格是 5.50。当客户下订单时,他们会用盒子运送咖啡。这些盒子有三种尺寸。大号(含 20 袋,中号(含 10 袋),小号(含 5 袋)。

大箱子的成本是 $1.80 中 $1.00 小 $0.60 例如,订单以最便宜的方式发货。包装的内容是把大中型箱子装满。也就是说,盒子已经装满了。只有小盒子可以有空格。但这不会让第三个盒子完全装满。开发一个计算订单总成本的程序。显示以下格式:

订购的行李数量:52 - 286.00 美元

使用的盒子 2 大 - 3.60 1 中 - 1.00 1 小 - 0.60

您的总费用为:291.20 美元

示例代码

    import javax.swing.*;
    import java.lang.*;
    import java.text.DecimalFormat;
    import java.util.*;
    import java.io.*;
    import java.math.*;

    public class CoffeeBags { 

    public static void main(String [] args) throws IOException
    {
        DecimalFormat df = new DecimalFormat ("#,##0.00");

        BufferedReader bufReader = new BufferedReader(new InputStreamReader(System.in));                  

        int numberOfBags;
        int largeBags = 2;
        int mediumBags = 1;
        int smallBags = 1;
        double costOfLargeBags = 3.60;
        double costOfMediumBags = 1.00;
        double costOfSmallBags = 0.60;
        double costPerBag = 5.50;
        double totalCost;
        double totalCostWithBags;

        System.out.print("Enter number of bags to order: ");
        String numberOfBagsStr = bufReader.readLine();
        numberOfBags = Integer.parseInt(numberOfBagsStr);


        totalCost = numberOfBags * costPerBag;
        System.out.println("Number of bags ordered: " +  numberOfBags + " - $" +       df.format(totalCost));

        System.out.println("Boxes used:");
        System.out.println("    " + largeBags   + " Large   -  $" +      df.format(costOfLargeBags));
        System.out.println("    " + mediumBags + "  Medium   -  $" + df.format(costOfMediumBags));
        System.out.println("    " + smallBags + "   Small   -  $" + df.format(costOfSmallBags));
        //System.out.print("Your total cost is: $ " + (totalCostWithBags));
        //String numberOfUnitsStr = bufReader.readLine();
        //numberOfUnits = Integer.parseInt(numberOfUnitsStr);

        System.out.println("\nPress any key to continue. . .");

        System.exit(0);
    }
}
4

2 回答 2

2

根据您的变量名称,您误解了练习。您有 -boxes- 可以容纳 20/10/5 个袋子,具体取决于尺寸;大中号箱子一定要满,小箱子不用。

所以你把所有的袋子除以20,把那么多放在大盒子里;然后取余数,除以 10,然后将余数放入小盒子中。

在这里寻找运营商;除以int另一个int总是四舍五入。

于 2013-07-02T01:37:33.850 回答
1

我想出了以下代码来实现您的目标。在这种情况下,使用整数向下舍入非常有用。您还需要正确命名变量。它们是装入盒子的袋子,而不是装入袋子的袋子。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;

public class CoffeeBags {
public static void main(String [] args) throws IOException {
    DecimalFormat df = new DecimalFormat ("#,##0.00");

    BufferedReader bufReader = new BufferedReader( 
                       new InputStreamReader(System.in));

    int             numberOfBags = 0;
    int             numberOfBoxes = 0;
    int             numberLargeBoxes = 0;
    int             numberMediumBoxes = 0;
    int             numberSmallBoxes = 0;
    double          costOfLargeBox = 1.80;
    double          costOfMediumBox = 1.00;
    double          costOfSmallBox = 0.60;
    double          totalCostLargeBoxes;
    double          totalCostMediumBoxes;
    double          totalCostSmallBoxes;
    double          totalBoxCost;
    double          costPerBag = 5.50;
    double          totalBagCost;
    double          totalCostWithBags;

    System.out.print("Enter number of bags to order: ");
    String numberOfBagsStr = bufReader.readLine();
    try {
        numberOfBags = Integer.parseInt(numberOfBagsStr);
    } catch (NumberFormatException e) {
        System.out.println("Error: Enter digits only");
    }


    totalBagCost = numberOfBags * costPerBag;
    if (numberOfBags > 20) {
        numberLargeBoxes = numberOfBags/20;
    } 
    if (numberOfBags - (numberLargeBoxes*20) < 20 || numberLargeBoxes == 0) {
        numberMediumBoxes = (numberOfBags - (numberLargeBoxes*20))/10;
    }
    if (numberOfBags - (numberLargeBoxes*20) - (numberMediumBoxes*10) < 10 ||
            numberLargeBoxes == 0 || numberMediumBoxes == 0) {
        numberSmallBoxes = (numberOfBags - (numberLargeBoxes*20) - (numberMediumBoxes*10))/5;
    }
    if (numberOfBags - (numberLargeBoxes*20) - (numberMediumBoxes*10) - 
            (numberSmallBoxes*5) < 5 && numberOfBags - (numberLargeBoxes*20) - (numberMediumBoxes*10) - 
            (numberSmallBoxes*5) > 0 || numberLargeBoxes == 0 || numberMediumBoxes == 0) {
        numberSmallBoxes++;
    }

    totalCostLargeBoxes = numberLargeBoxes*costOfLargeBox;
    totalCostMediumBoxes = numberMediumBoxes*costOfMediumBox;
    totalCostSmallBoxes = numberSmallBoxes*costOfSmallBox;
    totalBoxCost = totalCostLargeBoxes + totalCostMediumBoxes + totalCostSmallBoxes;
    totalCostWithBags = totalBoxCost + totalBagCost;



    System.out.println("Number of bags ordered: " +  numberOfBags + " - $" + df.format(totalBagCost));

    System.out.println("Boxes used: ");
    System.out.println("    " + numberLargeBoxes + "   Large   -  $" +      df.format(totalCostLargeBoxes));
    System.out.println("    " + numberMediumBoxes + "  Medium   -  $" + df.format(totalCostMediumBoxes));
    System.out.println("    " + numberSmallBoxes + "   Small   -  $" + df.format(totalCostSmallBoxes));
    System.out.println("Your total cost is: $ " + df.format(totalCostWithBags));

}
}
于 2013-07-02T02:28:07.453 回答