2

私は自分のプログラムで、3 つの商品の名前と数量と価格を含む請求書を出力させようとしています。すべてが正常に機能します。必要なのは、数値がどんなに大きくても、すべての小数を毎回並べるために価格と合計をフォーマットする方法だけです。これが私のコードです

    import java.util.Scanner;
    class AssignmentOneTest {

public static void main(String[] args)
{
    Scanner kb = new Scanner(System.in);

    //       System.out.printf("$%4.2f for each %s ", price, item);
    //       System.out.printf("\nThe total is: $%4.2f ", total);

    //process for item one
    System.out.println("Please enter in your first item");
    String item = kb.nextLine();
    System.out.println("Please enter the quantity for this item");
    int quantity = Integer.parseInt(kb.nextLine());
    System.out.println("Please enter in the price of your item");
    double price = Double.parseDouble(kb.nextLine());




    //process for item two
    System.out.println("Please enter in your second item");
    String item2 = kb.nextLine();
    System.out.println("Please enter the quantity for this item");
    int quantity2 = Integer.parseInt(kb.nextLine());
    System.out.print("Please enter in the price of your item");
    double price2 =Double.parseDouble(kb.nextLine());
    double total2 = quantity2*price2;
    //       System.out.printf("$%4.2f for each %s ", price2, item2);
    //       System.out.printf("\nThe total is: $%4.2f ", total2);

    //process for item three
    System.out.println("Please enter in your third item");
    String item3 = kb.nextLine();
    System.out.println("Please enter the quantity for this item");
    int quantity3 = Integer.parseInt(kb.nextLine());
    System.out.println("Please enter in the price of your item");
    double price3 = Double.parseDouble(kb.nextLine());
    double total3 = quantity3*price3;
    //       System.out.printf("$%4.2f for each %s ", price3, item3);
    //       System.out.printf("\nThe total is: $%4.2f ", total3);


    double total = quantity*price;

    double grandTotal = total + total2 + total3;
    double salesTax = grandTotal*(.0625);
    double grandTotalTaxed = grandTotal + salesTax;


    String amount = "Quantity";
    String amount1 = "Price";
    String amount2 = "Total";
    String taxSign = "%";

    System.out.printf("\nYour bill: ");
    System.out.printf("\n\nItem");
    System.out.printf("%28s %11s %11s", "Quantity", "Price", "Total");

    //complete item one format
    System.out.printf("\n%-30s", item);
    System.out.printf("%-10d", (int)quantity);
    System.out.printf("%-10.2f", (float)price);
    System.out.printf("  " + "%-10.2f", (float)total);

    //complete item two format
    System.out.printf("\n%-30s", item2);
    System.out.printf("%-10d", (int)quantity2);
    System.out.printf("%-10.2f", (float)price2);
    System.out.printf("  " + "%-10.2f", (float)total2);

    //complete item three format
    System.out.printf("\n%-30s", item3);
    System.out.printf("%-10d", (int)quantity3);
    System.out.printf("%-10.2f", (float)price3);
    System.out.printf("  " + "%-10.2f", (float)total3);




    System.out.printf("\n\n\nSubtotal %47.2f", grandTotal);
    System.out.printf("\n6.25 %s sales tax %39.2f", taxSign, salesTax);
    System.out.printf("\nTotal %50.2f", grandTotalTaxed);



}

問題は、価格が同じであるたびにすべてが一致することですが、たとえば、2 つの異なるアイテムに対して価格 50.00 と価格 2.50 を入力すると、アイテムの価格と小数点以下の合計がすべて一致するとは限りません。 、 助けてください。

4

2 に答える 2

8

タイトル用とデータ用の一致する関数のペアで出力を行うと、タイトルと列を並べるのがはるかに簡単になることがわかりました。次に例を示します。

public static void prLine (String item, int quantity, double price, double total) {
    System.out.printf("\n%-20.20s %10d %10.2f %10.2f", item, quantity, 
            price, total);
}   

public static void prTitles () {
    System.out.printf("\n%-20s %10s %10s %10s", "Item", "Quantity", 
            "Price", "Total");
}

この方法でフィールド幅を適切に対応させるのは簡単であることがわかります。次に、これらの関数を次のように使用できます。

prTitles ();
prLine (item,quantity,price,total);
prLine (item2,quantity2,price2,total2);
prLine (item3,quantity3,price3,total3);

...そして、あなたが探していると思うスタイルで並べられた出力を取得します:

Your bill:
Item                   Quantity      Price      Total
first                         1       1.50       1.50
second                       10      12.50     125.00 
third                       456     322.00  146832.00

出力コードを関数に入れると、main()関数内のコードの行数も大幅に削減されます。

于 2013-01-20T05:19:32.660 に答える
2

これを自分で制御する必要があります。

基本的に、これを処理するには2つの異なる方法があると考えています。

最初の方法は、出力する前に、必要なものをすべて文字列に変換してから長さを確認することで、出力の長さを確認することです。その後、価格の間にスペースを追加して、価格を並べることができます。このようなものはそれを達成できるかもしれませんが、もちろん統合されていますが、必要に応じて:

int length = String.valueOf(1000d).length();

私が考えている 2 番目の方法は、価格の間にタブを追加して、自動的に整列させることです。もちろん、この方法では、ほとんどの場合、すべての出力の間に余分なスペースがあり、アイテム名が 2 つ以上のタブが必要になるほど長くないようにする必要があります。

幸運を!さらに明確にする必要がある場合は、お知らせください。

編集: 少し改善するために、上記の長さチェックを組み込み、printf の幅指定子を使用してスペースを埋めることができます。それは少し良いです。

// calculate padding based on the length of the output
String format = "%" + padding + "d";
System.out.printf(format, variables);

EDIT2:これのOOPバージョン、完璧ではありませんが、非常にうまく機能します:)

EDIT3: コードにいくつかのコメントを追加しました。

http://pastebin.com/CqvAiQSg

于 2013-01-20T04:44:55.903 に答える