gpt4 book ai didi

java - 如何在此 Java 代码中使用数组,以便我可以输出值,以便将它们分配给位置?

转载 作者:行者123 更新时间:2023-11-30 08:29:30 25 4
gpt4 key购买 nike

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

这是主类,我有一个带有运行 userInput() 和 menu() 的 main() 的测试器类,但没有必要将其放入,因为它只有 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

最佳答案

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

首先,你真的不需要数组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- 你的原始代码中有错误,在构造函数中,你已将数组声明为局部变量,但无论如何使用了错误的名称,这将使实例字段未初始化,引发 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);

一些反馈 ;)

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

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

看看this example一些想法;)

关于java - 如何在此 Java 代码中使用数组,以便我可以输出值,以便将它们分配给位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19394047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com