0

私は本、独学で練習しています。それは私の心を吹き飛ばしています。誰かが私を助けることができたら?

1袋目、2袋目、3袋目を差し引いて合計個数を集計するにはどうすればよいですか?

プロジェクトはこれらの基準を尋ねます > 割り当ての詳細 会社はコーヒーを 2 ポンド袋でのみ販売しています。1 袋あたりの価格は 5.50 です。顧客が注文すると、コーヒーはボクセで発送されます。ボックスには 3 つのサイズがあります。大(20袋入)、中(10袋入)、小(5袋入)です。

大きな箱の価格は $1.80 中 $1.00 小 $0.60 注文は、たとえば、最も安価な方法で発送されます。梱包の仕方は、大箱と中箱をいっぱいに詰めることです。つまり、ボックスは完全に梱包されています。空きスペースを持つことができるのは小さなボックスだけです。しかし、それでは 3 番目のボックスが完全に梱包されたままになることはありません。注文の総コストを計算するプログラムを開発します。次の形式で表示します。

注文したバッグの数: 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

変数名に基づいて、演習を誤解しています。サイズに応じて、20/10/5 バッグを収納できるボックスがあります。大および中のボックスはいっぱいにする必要がありますが、小さなものはいっぱいにする必要はありません。

つまり、袋の総数を 20 で割り、その数を大きな箱に入れます。余りを 10 で割り、余りを小さな箱に入れます。

オペレーターについてはこちらをご覧ください。intを別のもので割るintと、常に切り捨てられます。

于 2013-07-02T01:37:33.850 に答える
1

あなたの目標を達成する次のコードを思いつきました。この状況では、int を使用して切り捨てを行うと非常に便利です。また、変数に正しく名前を付ける必要があります。それらは箱に入る袋であり、袋に入る袋ではありません。

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 に答える