【问题标题】:How to use modular division?如何使用模块化划分?
【发布时间】:2013-07-02 01:01:13
【问题描述】:

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

第一个、第二个和第三个袋子的金额如何减去并收取总和 包数?

项目要求这些标准 > 作业详情 该公司只出售 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);
    }
}

【问题讨论】:

    标签: java integer-division


    【解决方案1】:

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

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

    here 中查找运算符;将int 除以另一个int 将始终向下舍入。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 这当然有效,但是如果您只是使用一个变量来表示您在每个 if 中更改的包的数量,您的代码会看起来更好;这将摆脱所有大量的条件语句。 (另外我想,既然他在学习,我不应该只给出完整的代码答案,这对我来说似乎没有多大帮助。)
      • 是的,很公平。如果事情需要变得更复杂或者我需要添加功能,我总是在开发我的应用程序时向未来致敬。这就是为什么我决定使用巨大的条件语句。我也考虑了完整的代码答案,但有时将答案摆在您面前并弄清楚如何从您所在的位置获得解决方案会有所帮助。我发现这样我学得更好。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-09
      • 1970-01-01
      • 1970-01-01
      • 2012-01-25
      • 1970-01-01
      • 1970-01-01
      • 2013-06-25
      相关资源
      最近更新 更多