0

我有以下代码:

import java.util.Scanner;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.util.Arrays;


public class UserInterface
{
    public static void main(String[] args) throws FileNotFoundException {
        Scanner in = new Scanner(System.in); // for user input
        Scanner filein = new Scanner(new FileReader("products.txt")); // file of product information

        final int maxProducts = 10; // change this for maximum number of products in the catalog
        Product[] catalog = new Product[maxProducts]; // product information
        int productCount; // number of products read in from the file



        /*
         * You will need to add additional arrays and variables here.
         * The quantity array is given as an example:
         */
        int[] quantity = new int[maxProducts];
        double[] totalPrice = new double[maxProducts];
        int[] large = new int[maxProducts];
        int[] medium = new int[maxProducts];
        int[] small = new int[maxProducts];
        double[] shippingCost;
        double[] discount;

        // Read in the products from the file
        productCount = 0;
        while (filein.hasNext()) {
            catalog[productCount] = new Product();
            catalog[productCount].setName(filein.next());
            catalog[productCount].setUPC(filein.nextInt());
            catalog[productCount].setPrice(filein.nextDouble());
            catalog[productCount].setLargeCapacity(filein.nextInt());
            catalog[productCount].setMediumCapacity(filein.nextInt());
            productCount++;
        }

        // zero out all the totals and amounts to start a new order
        /* 
         * You will have to zero out all the quantity and total arrays and variables at the start
         * of each new order, or else you will end up with incorrect amounts.
         * The line below shows an easy way to fill an array with zeros, no matter how long it is:
         */
        Arrays.fill(quantity, 0);
        Arrays.fill(totalPrice, 0);
        Arrays.fill(large, 0);
        Arrays.fill(medium, 0);
        Arrays.fill(small, 0);


///////////// THIS IS LINE 56 ///////////////    

        // Ask the user to enter a quantity for each product, calculate totals and shipping boxes
        /* 
         * You will want to do this in a loop that goes through the array of products
         * (remember that the array may not be full - so you may not need to go to the end).
         * You should print out information about each product, then ask for a quantity for
         * that product, and store it in the quantity array.
         * Remember that you can call methods on the product objects even when they are in the
         * array e.g. catalog[i].getName()
         */
    for (int i = 0; i < catalog.length; i++){
          if (catalog[i] != null){
        System.out.println(catalog[i].getName());
          }
    }

        // Print the invoice line for each product ordered
        /*
         * You will want to do this in a loop that goes through the array of products.
         * Skip over products that were not ordered i.e. quantity is zero.
         */

        // Print the totals at the bottom of the invoice

        // Calculate the discounts and the final amounts, and print them

    }
}

请转到上面代码中标记的第 56 行。这就是我正在研究的问题。我在下面有我的循环。它可以编译等,但我不确定这是否能满足问题的要求。

编辑:第 56 行开始像“要求用户输入每个产品的数量......”

4

2 回答 2

2

您正在显示输出,但您没有向用户询问输入数量。您将需要访问 System.In 并读取用户输入的数量。此外,当评论说您不应该时,您仍在迭代整个循环。通过检查 null,您走在正确的轨道上,但是一旦遇到 null,您应该做一些事情来结束循环。

于 2012-05-01T16:28:30.227 回答
0

我会使用扫描仪进行输入。在我看来,您只是在输出目录数组的内容。如果您想输入金额,这可能会有所帮助。

for (int i = 0; i < catalog.length; i++){
      if (catalog[i] != null){
    System.out.println(catalog[i].getName());
    System.out.print("Quantity: ");
    quantity[i] = in.nexInt();}

      }
于 2012-05-03T00:37:58.407 回答