学校的java购物目录

时间:2014-05-05 22:06:38

标签: java

所以我必须为学校的作业创建目录。目录必须采取3个产品并将其存储在一个数组中,然后输入产品代码和产品数量来给出价格。输入“0”应该结束程序。在程序结束之前,它应该打印总金额。

我无法获得打印的总金额。它一直给我0.0。我该怎么办?

这是我的代码

package catalog;


 import java.util.*;
 public class Catalog1 {
 private static final String[] products = new String[3];
 private static float total=0.0f; 
 private static final float tax = 15/100;
 private static final float[] subTotal = new float[3];
 private static final float[] itemTotal = new float[3];
 private static final double[]pPrice = new double[3];
 private static String pCode;
 public static void main(String[] args) {
        products[0] = "Condensed Milk"; 
         products[1] = "Distilled Water";
         products[2] = "Packed rice";
         pPrice[0] = (9.50);
         pPrice[1] = (3.00);
         pPrice[2] = (12.75);
        int quantity = 0;
        int orderNum = 0;
     Scanner s = new Scanner(System.in);  



         do{ 
                System.out.println("------------------------------------------");
                System.out.println("condensed Milk[M3487], $9.50 per can.");
                System.out.println("");
                System.out.println("Distilled Water [W3876], $3.00 a bottle.");
                System.out.println("");
                System.out.println("packed Rice [R9983], $12.75 for 5lbs.");
                System.out.println("------------------------------------------");

                     System.out.println("Please enter order number (0 to stop)");
                                pCode = s.nextLine();

                                        if(pCode.equals("M3487")){
                                            System.out.println("condensed Milk $9.50");
                                                System.out.println("Enter Quantity");
                                                    quantity = s.nextInt();
                                                       itemTotal[0] = (float) (pPrice[0]*quantity); 
                                                        System.out.println(quantity + " condensed Milk @ $9.50 = $" + itemTotal[0]);

                                        }else if(pCode.equals("W3876")){
                                             System.out.println("Distilled Water $3.00");
                                                System.out.println("Enter Quantity");
                                                    quantity = s.nextInt();
                                                        itemTotal[1] = (float) (pPrice[1] * quantity);
                                                        System.out.println(quantity +" Distilled Water @ 3.00 = $" + itemTotal[1]);

                                        }else if(pCode.equals("R9983")){
                                             System.out.println("packed Rice $12.75");
                                               System.out.println("Enter Quantity");
                                                   quantity = s.nextInt();
                                                        itemTotal[2] = (float) (pPrice[2] * quantity);
                                                        System.out.println(quantity +" Distilled Water @ 3.00 = $" + itemTotal[2]);

                                            }else if(pCode.equals("0")){
                                                System.out.println("your total bill is $" + total);
                                                break;
                                            }//close if statement
                                        orderNum++;
                                        quantity++;


            }while(true);//close while loop

}//close main method

public static float totalBill() {


           subTotal[0] = itemTotal[0] * tax;
           subTotal[1] = itemTotal[1] * tax;
           subTotal[2] = itemTotal[2] * tax;
           total = subTotal[0] + subTotal[1] + subTotal[2] + itemTotal[0] +     itemTotal[1] + itemTotal[2];
    return total ; 

}//close bill method
    }//close class

输出显示此


浓缩牛奶[M3487],每罐9.50美元。

蒸馏水[W3876],每瓶3.00美元。

包装大米[R9983],5磅,12.75美元。


请输入订单号(0表示停止) M3487 炼乳$ 9.50 输入数量 2 2炼乳@ $ 9.50 = $ 19.0


浓缩牛奶[M3487],每罐9.50美元。

蒸馏水[W3876],每瓶3.00美元。

包装大米[R9983],5磅,12.75美元。


请输入订单号(0表示停止)


浓缩牛奶[M3487],每罐9.50美元。

蒸馏水[W3876],每瓶3.00美元。

包装大米[R9983],5磅,12.75美元。


请输入订单号(0表示停止)

1 个答案:

答案 0 :(得分:0)

解释评论:

private static final float tax = 0.15f;

System.out.println("your total bill is $" + totalBill());