2016-05-15 6 views
-1

私のプログラムでは、商品の説明、数量、価格をチャートのような形式で表示する必要があります。つまり、商品1の説明は価格と数量に沿ったものになります。これまでのところ、私はインターネット上で見つけたいくつかの方法を試しましたが、成功しませんでした。私はDr. Javaを使用していますので、そのコンパイラと互換性のあるものをお勧めします。 ありがとうございます!ここで文字列をJavaのチャートに書式設定する最も単純ではあるが最も効果的な方法は何ですか?

は、私がこれまで持っているものです。

public static void main(String []args){ 
    Scanner input=new Scanner(System.in); 
    String sentinel = "End"; 

    String description[] = new String[100]; 

    int quantity[] = new int[100]; 

    double price [] = new double[100]; 
    int i = 0; 
    // do loop to get input from user until user enters sentinel to terminate data entry 
    do 
    { 
    System.out.println("Enter the Product Description or " + sentinel + " to stop"); 
    description[i] = input.next(); 

    // If user input is not the sentinal then get the quantity and price and increase the index 
    if (!(description[i].equalsIgnoreCase(sentinel))) { 
     System.out.println("Enter the Quantity"); 
     quantity[i] = input.nextInt(); 
     System.out.println("Enter the Product Price "); 
     price[i] = input.nextDouble(); 
    } 
    i++; 
    } while (!(description[i-1].equalsIgnoreCase(sentinel))); 


    // companyArt(); 
    //System.out.print(invoiceDate()); 
    //System.out.println(randNum()); 



    System.out.println("Item Description: "); 
    System.out.println("-------------------"); 
    for(int a = 0; a <description.length; a++){ 
    if(description[a]!=null){ 
     System.out.println(description[a]); 
    } 
} 
    System.out.println("-------------------\n"); 


    System.out.println("Quantity:"); 
    System.out.println("-------------------"); 
    for(int b = 0; b <quantity.length; b++){ 
    if(quantity[b]!=0){ 
     System.out.println(quantity[b]); 
    } 
    } 
    System.out.println("-------------------\n"); 

    System.out.println("Price:"); 
    System.out.println("-------------------"); 
    for(int c = 0; c <price.length; c++){ 
    if(price[c]!=0){ 
     System.out.println("$"+price[c]); 
    } 
    } 
    System.out.println("-------------------"); 

    //This is where I multiply the price and quantity together to get the total 
    double total = 0.0; 
    for (int j = 0; j < quantity.length; j++){ 

    total += quantity[j] * price[j]; 
    } 

    if(total != 0){ 
    System.out.println("Total: " + total); 
    }  
    } 
} 
+1

フォーマッタを使用してください:) –

+1

* "Dr.Javaを使用していますので、そのコンパイラと互換性のあるものをお勧めします。" * - コンパイラは無関係です。 –

+2

出力の例を挙げてください。私たちは推測しようとすることができますが、なぜ私たちはそうするべきですか?あなたは私たちの助けが必要なので、あなたが必要とするものに特化してください。そして、あなたは問題を抱えています。 – Andreas

答えて

0

あなたのコードでは、価格のリストが続く数量のリストが続く項目記述のリストを、プリントアウト。私はこれがあなたがそれを見たいと思っていないと仮定しています。最良の解決策は、ループごとにこれらの3つの事柄のそれぞれを印刷する単一のforループを使用することです。次のコードは、アイテムを表す各列と、三つの「アイテムの説明」、「数量」とラベル付けされた列、及び「価格」とテーブルをプリントアウトし、各列を分離する破線:

System.out.println("Item Description:\tQuantity:\tPrice:\t"); 
System.out.println("---------------------------------------------------------"); 
for(int a = 0; a <description.length; a++){ 
    if (description[a].equalsIgnoreCase("end")) { 
     break; 
    } 
    System.out.println(description[a] + "\t\t" + quantity[a] + "\t\t" + price[a]); 
    System.out.println("---------------------------------------------------------\n"); 
} 

出力がされますこのようにフォーマット:

Item Description: Quantity: Price: 
--------------------------------------------------------- 
asdfaoiew;rjlkf  248   4309.0 
--------------------------------------------------------- 

asodifaasd   43   2323.0 
--------------------------------------------------------- 

asdfasoif   234   2.0 
--------------------------------------------------------- 

列が正しく並ばない場合は、あなたが追加したり、アイテムの説明がいかに長いか短いに応じて、説明した後、1「\ tを」削除するか必要があります。

関連する問題