我正在练习一本书,自学。 这让我大吃一惊。如果有人可以帮助我?
如何减去第一个,第二个和第三个包的数量以及收集总数 包的数量?
项目要求这些标准> 作业细节 该公司仅以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);
}
}
答案 0 :(得分:2)
根据您的变量名称,您误解了练习。你有 - 箱子 - 可以容纳20/10/5袋,具体取决于尺寸;大中型箱必须装满,小型装箱不需要。
所以你拿整个袋子,除以20,把那么多放在大盒子里;然后取余数,除以10,然后其余部分进入小方框。
查看运营商here;将int
除以另一个int
将始终向下舍入。
答案 1 :(得分: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));
}
}