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 に答える