如何在Java中输出多个不同的用户输入值?

时间:2018-02-01 03:40:10

标签: java loops input do-while

我希望下面这个程序捕获用户输入(第一个产品名称,然后成本),然后输出到控制台,并询问用户是否还想要其他任何东西,如果他们这样做,它会再次执行并输出下一个产品和成本。

如果用户回答“否”,那么我希望它按编号和名称输出项目列表,然后输出每个项目请求的总成本,然后是总体总成本。

到目前为止,这是我的代码;我想了解如何获得总体总成本并列出每个项目。我觉得我很亲密。

public static void main(String[] args) {

    /////////Initialize everything here/////////
    Scanner keyboard = new Scanner (System.in);
    String nameProd;
    String response;
    int items = 0;
    int costMat;
    int hoursReq;
    int payPerHr = 15; //cost per hour for only one employee, who is also the owner (me)
    double shipping = 13.25; //shipping cost remains constant even with multiple items
    //////////////////////////////////////////////////////////////////////////////////

    System.out.println("================================="
        + "\nWelcome to Ryan's Computer Store!"
        + "\n=================================");

    do{
        items++;
            //////////////////////////////////////////    
            System.out.print("Enter product name: ");
                nameProd = keyboard.next();
                    ////////////////////////////////////////////////
                    System.out.print("Enter cost of materials: $");
                        costMat = keyboard.nextInt();
                    System.out.print("In hours, how soon would you prefer that this order is completed?: ");
                        hoursReq = keyboard.nextInt();
                        //////////////////////////////////////////////////////////////////////////////////////////
                        System.out.println("====================================================================" 
                            + "\n============================" 
                            + "\n>>>>>>Rundown of costs<<<<<<" 
                            + "\nItem #: " + items
                            + "\nItem Name: " + nameProd 
                            + "\nCost of Materials: $" + costMat 
                            + "\n===>Hours spent creating the product: " + hoursReq + " hours" 
                            + "\n===>Employee Pay Per Hour: $" + payPerHr);
                        int priceMarkup = hoursReq*payPerHr;
                            //////////////////////////////////////////////////////
                            System.out.println("Price of product after markup: $" 
                                + (priceMarkup+costMat));
                                //////////////////////////////////////////////////////
                                System.out.println("===>Shipping Fee: $" + shipping);
                                    //////////////////////////////////////////////
                                    int costBeforeShipping = priceMarkup+costMat;
                                    double totAmt = shipping+costBeforeShipping;
                                        //////////////////////////////////////////////////////
                                        System.out.println("Amount to be charged for item #" + items + " (" + nameProd + ")" + ": $" + totAmt
                                            + "\n============================");
                                                //////////////////////////////////////////////////////////////////////////////
                                                System.out.print("========================================================" 
                                                    + "\nIs there anything else that you would like to order?: ");
                                                    response = keyboard.next();
                                                    }
    while
        (response.equalsIgnoreCase("yes"));
    System.out.println(">>>>>========================================================<<<<<\nTOTAL AMOUNT TO BE CHARGED FOR " + items + " ITEMS: " + "\nShipping (flat fee): " + shipping + "\nSum of Items: ");
}}

1 个答案:

答案 0 :(得分:0)

您需要一个列表来保存项目名称和一个临时变量来保存价格总和。我认为以下代码可以帮助您。

 Scanner keyboard = new Scanner (System.in);
        String nameProd;
        String response;
        int items = 0;
        int costMat;
        int hoursReq;
        int payPerHr = 15; //cost per hour for only one employee, who is also the owner (me)
        double shipping = 13.25; //shipping cost remains constant even with multiple items
        //////////////////////////////////////////////////////////////////////////////////

        List<String> orderItems = new ArrayList<>();
        double totalPrice=0;


        System.out.println("================================="
            + "\nWelcome to Ryan's Computer Store!"
            + "\n=================================");

        do{
            items++;
                //////////////////////////////////////////    
                System.out.print("Enter product name: ");
                    nameProd = keyboard.next();
                        ////////////////////////////////////////////////
                        System.out.print("Enter cost of materials: $");
                            costMat = keyboard.nextInt();
                        System.out.print("In hours, how soon would you prefer that this order is completed?: ");
                            hoursReq = keyboard.nextInt();
                            //////////////////////////////////////////////////////////////////////////////////////////
                            System.out.println("====================================================================" 
                                + "\n============================" 
                                + "\n>>>>>>Rundown of costs<<<<<<" 
                                + "\nItem #: " + items
                                + "\nItem Name: " + nameProd 
                                + "\nCost of Materials: $" + costMat 
                                + "\n===>Hours spent creating the product: " + hoursReq + " hours" 
                                + "\n===>Employee Pay Per Hour: $" + payPerHr);

                            orderItems.add(nameProd);


                            int priceMarkup = hoursReq*payPerHr;
                                //////////////////////////////////////////////////////
                                System.out.println("Price of product after markup: $" 
                                    + (priceMarkup+costMat));
                                    //////////////////////////////////////////////////////
                                    System.out.println("===>Shipping Fee: $" + shipping);
                                        //////////////////////////////////////////////
                                        int costBeforeShipping = priceMarkup+costMat;
                                        double totAmt = shipping+costBeforeShipping;
                                        totalPrice+=totAmt;
                                            //////////////////////////////////////////////////////
                                            System.out.println("Amount to be charged for item #" + items + " (" + nameProd + ")" + ": $" + totAmt
                                                + "\n============================");
                                                    //////////////////////////////////////////////////////////////////////////////
                                                    System.out.print("========================================================" 
                                                        + "\nIs there anything else that you would like to order?: ");
                                                        response = keyboard.next();
                                                        }
        while
            (response.equalsIgnoreCase("yes"));
        System.out.println(">>>>>========================================================<<<<<\nTOTAL AMOUNT TO BE CHARGED FOR  ITEMS: " + orderItems +  "\nShipping (flat fee): " + shipping + "\nSum of Items: "+totalPrice);
    }