Java的循环(做,而)麻烦

问题描述:

这里是我试图编程,但是所需其他资料来完成这件事,我需要最终的结果要做到这一点的代码中有相当:Java的循环(做,而)麻烦

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

和一个样本输出应该是这个样子:

产品单价数量小计Klout的自行车喇叭$ 7.19
2 $ 14.38条Garmin的先行者手表$ 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 
+3

什么问题?如果是“完成它”,你有没有尝试完成它? – Zong

+0

也许你必须在'do-while'循环中加入'if'子句。 – Math

+0

@ZhengZhengLi当试图停止询问产品然后产生输出时,执行程序时出现问题。还有第二堂课,这个应该与我最近一起编辑帖子并将其添加进去。 –

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

+0

不客气。 –