2

我是一个java初学者,我正在编写一个程序来获取商品的名称、商品的单位、价格/单位和总价。我正在尝试将这些值放在不同的数组中,以便稍后在创建某种收据时访问它们,但我不知道如何在数组位置分配这些值,然后访问这些相同的位置而无需硬编码. 循环是最好的选择,但我不知道如何设置这个东西。帮助和建议将不胜感激。请记住,我不能做高级的东西,比如矩阵和 3D 数组。如果你能保持简单,那就太棒了。

这是主类,我有一个带有 main() 的测试器类,它运行 userInput() 和 menu() 但没有意义,因为它只有 2 行代码。

import java.util.Scanner;
public class GroceryList {

    // instance variables 
    Scanner Price, Items, NumItems, Units;

    private double myGross, myNet;
    private final double STATETAX;
    private double totalPrice, unitPrice;
    private String itemName;
    private int totalUnits;
    ///////////////////////////////////////////// arrays I will use
    private double[] totalPrice1;
    private double[] unitPrice1;
    private String[] itemName1;
    private int[] totalUnits1;

    public GroceryList()
    {
        STATETAX = 0.06;
        double[] totalPrice = new double[50];
        double[] unitPrice = new double[50];
        String[] itemName = new String[50];
        int[] totalUnits = new int[50];
    }

    public void userInput()
    {
        Scanner Price = new Scanner(System.in);
        Scanner Items = new Scanner(System.in);
        Scanner Units = new Scanner(System.in);
        Scanner NumItems = new Scanner(System.in);

        int u, c, totalItems;// c is the number of items that has to equal totalItems in order for the loop to break
        double price;
        String item;//gets the name of the item
        c=0;

        System.out.println("Welcome to Grocery List ! \n");
        System.out.print("Enter the total number of items you want to buy (not total units !) : ");
        totalItems = NumItems.nextInt();
        System.out.println();

        do 
        {
            c++ ;
            System.out.print("Enter the item name : ");
            item = Items.nextLine();
            System.out.print("Enter the units of " + item + " you want to buy : ");
            u = Units.nextInt();
            System.out.print("Enter the price of a/n " + item + " : $");
            price = Price. nextDouble();

            /*this would make only one value appear at the receipt, which would be only one item name, one unit price, one price/unit, one total price and the other values calculated
   would not appear on the receipt because you cant assign more than 1 value to a variable so thats why I need arrays. 
             */
            itemName = item;
            totalUnits = u;
            unitPrice = price;



            calc(u,price,item);
        }
        while (c < totalItems);
    }

    public void calc(int u, double p, String i)
    {
        double total;//total amount of $ for 1 item
        total = u*p;
        System.out.println("Total price of " + i + " : $" + total + "\n");
        grossPay(total);
        totalPrice = total;
    }


    public void grossPay(double total)
    {
        double gross;
        myGross += total;
    }

    public double tax()
    {
        double temp;
        temp = myGross*STATETAX;
        myNet = myGross - temp;
        return myNet;
    }

    public void menu()
    {

        System.out.printf("%-10s %6s %11s %11s" , "ITEM :" , "UNITS :" , "PRICE :" , "TOTAL :"); 
        System.out.println();
        System.out.printf("%-11s %2d %7s $%4.2f %5s $%2.2f", itemName, totalUnits,"", unitPrice,"", totalPrice);
        System.out.println();



    }

    public void payment()
    {
        System.out.println("Amount before tax : $" + myGross);
        System.out.println("Amount after tax : $" + tax());

    }

}//end GroceryList
4

2 回答 2

2

让我们从一点重组开始;)

首先,你真的不需要数组totalPrice1,总价就是总价,只有一个......(恕我直言)

您应该在userInput方法中初始化它们,而不是在构造函数中初始化数组,这是当您知道用户想要输入多少项时。否则,如果我想输入 51 个项目,你会遇到问题;)

System.out.println("Welcome to Grocery List ! \n");
System.out.print("Enter the total number of items you want to buy (not total units !) : ");
totalItems = NumItems.nextInt();
System.out.println();

unitPrice1 = new double[totalItems];
itemName1 = new String[totalItems];
totalUnits1 = new int[totalItems];

(nb-您的原始代码中有错误,在构造函数中,您已将数组声明为局部变量,但以任何方式使用了错误的名称,这将使实例字段未初始化,引发 a NullPointerException

虽然这肯定不是错误,但c在循环结束时递增会更简单......

do {
    //...
    calc(u, price, item);
    c++;
} while (c < totalItems);

这意味着您不需要不断调整阵列的位置。

在您的“收集”循环中,您需要将用户输入的值分配给每个数组...

do {
    //...
    itemName1[c] = item;
    totalUnits1[c] = u;
    unitPrice1[c] = price;
    //...
} while (c < totalItems);

说了这么多,实际上使用for-next循环之类的东西会更容易......

for (int c = 0; c < totalItems; c++) {
    //...
}

恕我直言...

最后,当你准备好时,你可以通过简单地遍历数组来打印收据......

for (int index = 0; index < totalItems; index++) {
    double itemCost = unitPrice1[index] * totalUnits1[index];
    System.out.println(itemName1[index] + " @ " + unitPrice1[index] + " x " + totalUnits1[index] + " = " + cost);
}
System.out.println("Total Cost: " + totalPrice);

一些反馈;)

说了这么多,我个人会为自己创建一个简单的Object,其中包含所有必需的信息,例如;

public class ShoppingItem {
    public String name;
    public double unitPrice;
    public double quantity;
}

然后,您只需要一个数组,例如...

//private double[] unitPrice1;
//private String[] itemName1;
//private int[] totalUnits1;
private ShopingItem[] items;

然后,根据需要,您只需创建该项目的一个新实例并填写它,例如...

items[c] = new ShoppingItem();
items[c] = item;
items[c] = u;
items[c] = price;
//itemName1[c] = item;
//totalUnits1[c] = u;
//unitPrice1[c] = price;

打印收据看起来像......

for (ShoppingItem item : items) {
    double itemCost = item.unitPrice * item.quantity;
    System.out.println(item.name + " @ " + item.unitPrice + " x " + item.quantity + " = " + cost);
}
System.out.println("Total Cost: " + totalPrice);

对于更“高级”的输出,我鼓励您看一下类似String#format和/或System.out.printf

看看这个例子以获得一些想法;)

于 2013-10-16T02:16:42.160 回答
1

理想情况下,您会(在我误读您的排序含义时写了这个):

创建一个Item具有字段name, totalPrice, unitPrice,的对象totalUnits。然后在您的购物清单中,您不需要有 4 个数组,只需一个Items. 省去了必须跟踪索引的可疑任务。

如果您还创建了一个ItemComparator实现的类,Comparator您可以定义它们的排序方式,然后您可以使用

Collections.sort(Arrays.asList(itemsList), new ItemComparator());

您也不需要四个扫描仪,您可以使用同一个扫描仪,因为它们都在System.in

于 2013-10-16T02:12:20.537 回答