Java循环(do and while)麻烦

时间:2013-11-21 19:10:12

标签: java loops for-loop while-loop

这是我尝试编程的相当数量的代码但是还有什么需要完成此操作我需要最终结果来执行此操作:

  

使用以下输入编译并运行程序(提供输出   你的节目)a)第一个产品为“自行车喇叭”,单价为   “7.19”,数量为“2”,制造商为“Klout”。 b)第二   产品为“Forerunner Watch”,单价为“140.89”,数量为   “2”,制造商为“Garmin”。

     

示例OUTPUT应该如下所示:

     

产品单价价格数量总计Klout自行车号角$ 7.19
  2 $ 14.38 Garmin Forerunner手表$ 140.89 2 $ 281.78

//***************************************************************

// import statements

import java.util.ArrayList;
import java.text.NumberFormat;
import java.util.Scanner;

//Class header

public class ShoppingCart {

    // Start of main method

    public static <Item> void main(String[] args) {

        // Declare and instantiate a variable that is an ArrayList that can hold
        // Product objects

        ArrayList<Product> item = new ArrayList<Product>();

        // Declare necessary local variables here

        String Name = null;
        double Price = 0;
        int Quantity = 0;
        String Seller = null;

        Scanner scan = new Scanner(System.in);

        String Shop = "yes";

        // Write a print statement that welcome's the customer to our shop

        /**
         * 
         * create a do while that will be keep looping as long as user wants to
         * continue shopping
         */

        Product item1 = new Product(Name, Price, Quantity, Seller);

        // do while loop start

        do {

            // Ask user to enter product name and store it in appropriate local
            // variable

            System.out.print("Please Enter the Product Name: ");

            Name = scan.next();

            // Ask user to enter product price and store it in appropriate local
            // variable

            System.out.print("Please Enter the Price of the Product: ");

            Price = scan.nextDouble();

            // Ask user to enter quantity and store it in appropriate local
            // variable

            System.out.print("Please enter the Quantity: ");

            Quantity = scan.nextInt();

            // Ask user to enter product manufacturer name and store it in
            // appropriate local variable

            System.out.print("Please Enter the Manufacturer: ");

            Seller = scan.next();

            System.out.print("Would you like to continue shopping?");

            Shop = scan.next();

            // create a new Product object using above inputed values

            Product newitem = new Product(Name, Price, Quantity, Seller);

            // add above created Product to the ArrayList cart if Product has
            // available stock

            // if stock not available inform user requested product is out of
            // stock

            // Ask user whether wants to continue shopping

            // set the do while loop to continue to loop if Yes option is
            // selected

        } while (Shop.equals(Shop));
        {
            if (Shop == null) {
                break;
            }

            // do while loop end
            // header for shopping cart contents
            // print details of each product added to the ArrayList
            // calculate total price of the shopping cart
            // print the total price of the shopping cart
        }

    }// end of main method

}// end of Shop class



//***************************************************************
//Product.java
//Represents an item in a shopping cart.
//***************************************************************
import java.text.NumberFormat;

public class Product
{
    private String name;
    private double price;
    private int quantity;
    private double subtotal;
    private String manufacturer;
    private int inventory;

    // -------------------------------------------------------
    //  Create a new item with the given attributes.
    // -------------------------------------------------------
    public Product (String name, double price, int quantity, String manufacturer)
    {
        this.name = name;
        this.price = price;
        this.quantity = quantity;
        this.manufacturer = manufacturer;

        subtotal = price*quantity;

        inventory = 10;
    }

    // -------------------------------------------------------
    //   Return a string with the information about the Product
    // -------------------------------------------------------
    public String toString ()
    {
        NumberFormat fmt = NumberFormat.getCurrencyInstance();
        /*
        String item;
        if (name.length() >= 8)
            item = name + "\t";
        else
            item = name + "\t\t";
            */
        return (manufacturer + " " + name + "\t\t    " + fmt.format(price) + "\t    " + quantity 
                + "\t\t" + fmt.format(subtotal));
    }

    //   Returns the unit price of the Product
    public double getPrice()
    {
        return price;
    }

    //   Returns the name of the Product
    public String getName()
    {
        return name;
    }

    //   Returns the quantity of the Product
    public int getQuantity()
    {
        return quantity;
    }

    //   Returns the sub total of the Product
    public double getSubTotal()
    {
        return subtotal;
    }

    // Returns product manufacturer
    public String getManufacturer()
    {
        return manufacturer;
    }

    // Checks whether product is in stock
    // if after inventory get below stock after processing order, then
    // places order to replenish stock
    public boolean checkInventory()
    {
        boolean flag = false;
        if (inventory > quantity)
        {
            flag = true;
            inventory = inventory - quantity;

            if (inventory <= 0)
                placeOrder(quantity);
        }

        return flag;
    }

    // Replenishes stock to 10 times the quantity of last order
    private void placeOrder(int orderQuantity)
    {
        inventory = orderQuantity * 10;
    }
}//end of class Item  

1 个答案:

答案 0 :(得分:2)

你在循环的第一次迭代中覆盖Shop。这意味着Shop将始终等于Shop将打破循环。我假设预期的逻辑是检查覆盖商店的扫描是否等于“是”。如果是,那么让while循环检查Shop.equals(“是”)。为了将来参考,请在您的问题中包含更多信息,例如您做了哪些工作,实际问题是什么,并包括所需的类,以便其他人可以毫不费力地运行该程序。